Drag - Rotate
Please I need help to rotate a Game Object by dragging. The object is a rudder of a boat to rotate with the touch screen. I have a problem, clicking on the object always returns to angle 0. Then if I can make it rotate, but it always starts from 0 and not from where I stay.
public void OnClick()
{
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;
}
public void OnDrag()
{
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.right);
}
PLEASE HELP ME !!!
Comment
Your answer
Follow this Question
Related Questions
Simple Rotate Script won't work! 2 Answers
rotate in the direction of the child 1 Answer
2D project: Align a object's rotation to another object 0 Answers
Rotating on other axis 1 Answer
Align an object rotation in relation to another object 0 Answers