Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active September 25, 2025 05:23
Show Gist options
  • Select an option

  • Save unitycoder/5366a610599b503a40e9 to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/5366a610599b503a40e9 to your computer and use it in GitHub Desktop.

Revisions

  1. unitycoder revised this gist Feb 11, 2024. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions RotateSpriteTowardsMouse.cs
    Original 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
    // 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;
    }
    }
  2. unitycoder revised this gist Jun 19, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions RotateSpriteTowardsMouse.cs
    Original 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));

    }
    }
  3. unitycoder revised this gist Jun 24, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion RotateSpriteTowardsMouse.cs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    // Rotate sprite/object towards mouse : http://johnstejskal.com/wp/rotating-objects-and-sprites-in-unity3d-using-cs-c/
    // 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;
  4. unitycoder renamed this gist May 10, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. unitycoder created this gist Oct 30, 2014.
    35 changes: 35 additions & 0 deletions RotateSpriteTowardsMouse
    Original 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);
    }
    }