Skip to content

Instantly share code, notes, and snippets.

@elpsk
Created January 5, 2017 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elpsk/ff0795f34fdf772d85382cd2826b3e36 to your computer and use it in GitHub Desktop.
Save elpsk/ff0795f34fdf772d85382cd2826b3e36 to your computer and use it in GitHub Desktop.
Rotate object on Z axis in 2D environment, with mouse.
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