- Home /
Question by
pkulsy · Aug 30, 2016 at 03:00 PM ·
rotate objectmouse-drag
Rotating sprite with mouse
Hello, I have used the rotating code made by Aarlangdi (https://www.youtube.com/watch?v=PW42etn0PBw) Thanks to him. It works quite well with 3D object such as cube, but it cant work on sprite. Anyone know why is it? Appreciate much!
using UnityEngine; using System.Collections;
public class rotate : MonoBehaviour {
void OnMouseDown(){
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
baseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) *Mathf.Rad2Deg;
}
void OnMouseDrag(){
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
pos = Input.mousePosition - pos;
float ang = Mathf.Atan2(pos.y, pos.x) *Mathf.Rad2Deg - baseAngle;
transform.rotation = Quaternion.AngleAxis(ang, Vector3.forward);
}
}
Comment
I think you need to add Sphere or Capsule Collider to rotate sprites.