Created
January 5, 2017 11:33
-
-
Save elpsk/ff0795f34fdf772d85382cd2826b3e36 to your computer and use it in GitHub Desktop.
Rotate object on Z axis in 2D environment, with mouse.
This file contains 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
using UnityEngine; | |
using System.Collections; | |
public class Rotate : MonoBehaviour | |
{ | |
public GameObject objectToRotate; | |
public GameObject rotationOverlay; // optional | |
void OnMouseDrag () | |
{ | |
Vector3 pos = Camera.main.WorldToScreenPoint (objectToRotate.transform.position); | |
Vector3 dir = Input.mousePosition - pos; | |
float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg; | |
objectToRotate.transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward); | |
rotationOverlay.transform.rotation = objectToRotate.transform.rotation; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment