Forked from unitycoder/RotateSpriteTowardsMouse.cs
Last active
August 29, 2015 14:23
-
-
Save JT5D/1d4ce3b3259fc1625491 to your computer and use it in GitHub Desktop.
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 characters
| // 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); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment