Last active
September 25, 2025 05:23
-
-
Save unitycoder/5366a610599b503a40e9 to your computer and use it in GitHub Desktop.
Revisions
-
unitycoder revised this gist
Feb 11, 2024 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ // Rotate sprite/object towards mouse // Reference: http://johnstejskal.com/wp/rotating-objects-and-sprites-in-unity3d-using-cs-c/ // https://forum.unity3d.com/threads/how-to-detect-angle-from-one-object-to-another-in-2d.477510/ // Usage: Attach this script to sprite, use Orthographic camera! using UnityEngine; using System.Collections; @@ -37,6 +37,9 @@ void Update () // using lookat // transform.LookAt(target.position, new Vector3(0, 0, -1)); // can also set transform.up (green axis) //mousePos.z = 0; //transform.up = mousePos - transform.position; } } -
unitycoder revised this gist
Jun 19, 2017 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ // Rotate sprite/object towards mouse // Reference: http://johnstejskal.com/wp/rotating-objects-and-sprites-in-unity3d-using-cs-c/ // https://forum.unity3d.com/threads/how-to-detect-angle-from-one-object-to-another-in-2d.477510/ // Usage: Attach this script to sprite using UnityEngine; @@ -33,5 +34,9 @@ void Update () // rotate X (red axis) away from mouse //Vector3 perpendicular = Vector3.Cross(mousePos-transform.position,Vector3.forward); //transform.rotation = Quaternion.LookRotation(Vector3.forward, perpendicular); // using lookat // transform.LookAt(target.position, new Vector3(0, 0, -1)); } } -
unitycoder revised this gist
Jun 24, 2016 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,6 @@ // Rotate sprite/object towards mouse // Reference: http://johnstejskal.com/wp/rotating-objects-and-sprites-in-unity3d-using-cs-c/ // Usage: Attach this script to sprite using UnityEngine; using System.Collections; -
unitycoder renamed this gist
May 10, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
unitycoder created this gist
Oct 30, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ // Rotate sprite/object towards mouse : http://johnstejskal.com/wp/rotating-objects-and-sprites-in-unity3d-using-cs-c/ using UnityEngine; using System.Collections; public class RotateSpriteTowardsMouse : MonoBehaviour { private Camera cam; void Start () { cam = Camera.main; } void Update () { Vector3 mousePos = cam.ScreenToWorldPoint(Input.mousePosition); // rotate Y (green axis) towards mouse //transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position); // rotate Y (green axis) away from mouse //transform.rotation = Quaternion.LookRotation(Vector3.forward, transform.position-mousePos); // rotate X (red axis) towards mouse Vector3 perpendicular = Vector3.Cross(transform.position-mousePos,Vector3.forward); transform.rotation = Quaternion.LookRotation(Vector3.forward, perpendicular); // rotate X (red axis) away from mouse //Vector3 perpendicular = Vector3.Cross(mousePos-transform.position,Vector3.forward); //transform.rotation = Quaternion.LookRotation(Vector3.forward, perpendicular); } }