Basically I have an object rotating. It is a click and drag type of rotation, but when the object is facing the -z -x corner, or bottom left corner, it has a chance of completely flipping 180 degrees the opposite way when clicked again. This is very troublesome and I even know what line this takes place in. Here is the on-click code:
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 is giving me the biggest headache. I tried playing around with the ".x" and ".z" endings and I just can't figure it out. When I switch ".x" with ".z" the issue is then taking place in the bottom right corner. If I even mess with ".y" I only get further away from what I want.
Picture:
By the way the Z axis is going up vertically and the X axis is going right horizontally. Any help appreciated.
The problem seemed to be that I missed a combination of the .x, .y, and .z. When I tried replacing "transform.up.x" with "transform.up.y" I stopped having the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With