Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object flips 180 degrees on z-axis in -z and -x corner

Tags:

c#

unity3d

unity5

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.

like image 857
KOman l Avatar asked Dec 17 '15 00:12

KOman l


1 Answers

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.

like image 145
KOman l Avatar answered Oct 21 '22 12:10

KOman l