Basically, I have an object rotating. This is a click and drag type rotation, but when the object is facing the corner -z -x or the lower left corner, it has a chance to completely reverse the opposite path by 180 degrees when pressed again. This is very troublesome, and I even know in which line this is happening. Here is the code on click:
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.up.x) * Mathf.Rad2Deg; startRotation = transform.rotation; }
baseAngle - = Mathf.Atan2 (transform.right.y, transform.up.x) * Mathf.Rad2Deg;
^^^ This line gives me the biggest headache. I tried to play with the finals ".x" and ".z", and I just can't figure it out. When I switch ".x" to ".z", the problem occurs in the lower right corner. Even if I’m at war with “.y”, I’ll just get away from what I want.
Photo:

By the way, the Z axis rises vertically, and the X axis goes horizontally. Any help was appreciated.
source share