- Home /
Question by
keiranlovett · Nov 20, 2014 at 05:01 AM ·
ui4.6
Unity 4.6 - Rotating with Mouse Issue
I am trying to create a "dial" element, which can be used for various cases in Unity 4.6 UI. I have the dragging working, however for some reason, the rotation following the mouse is difficult, not letting the mouse rotate the button around a full 360.
public GameObject texture = null;
private float angle = 0;
private Vector2 pos = new Vector2(0, 0);
private Rect rect;
private Vector2 pivot;
private bool rotating = false;
private float initialMouseAngle;
void Start() {
rect = new Rect(texture.transform.localPosition.x - texture.GetComponent<RectTransform>().sizeDelta.x * 0.5f, texture.transform.localPosition.y - texture.GetComponent<RectTransform>().sizeDelta.y * 0.5f, texture.GetComponent<RectTransform>().sizeDelta.x, texture.GetComponent<RectTransform>().sizeDelta.y);
pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
}
void Update() {
Vector2 guiMouse = Input.mousePosition;
if (Input.GetMouseButtonDown(0)) {
Vector2 v2T = ((Vector2)guiMouse - pivot);
initialMouseAngle = Mathf.Atan2(v2T.y, v2T.x) - angle * Mathf.Deg2Rad;
rotating = true;
}
else if (Input.GetMouseButton(0) & rotating) {
Vector2 v2T = ((Vector2)guiMouse - pivot);
angle = (Mathf.Atan2 (v2T.y, v2T.x) - initialMouseAngle) * Mathf.Rad2Deg;
}
else if (Input.GetMouseButtonUp(0)) {
rotating = false;
}
texture.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
Comment
Your answer
Follow this Question
Related Questions
Showing text on top of a Slider, but not blocking Handler? 1 Answer
Android screen is small? 0 Answers
4.6 Button OnClick Super Sensitive to pointer movement. 1 Answer
Unity 4.6 - Detect Touch On Screen 1 Answer
OnPointerExit doesn't work 1 Answer