- Home /
Getting angle, 0-359, between two game objects
I can't seem to figure out how to get the angle between two game objects. I need the angle on the X,Y plane and the X,Z plane. I was trying transform.eulerAngles.y and transform.eulerAngles.z but there are scenarios where that does not work because if you go around 180 on XZ and then 180 on XY the XZ should be 0 but it stays at 180. What is the best way to get the correct degrees between the two game objects all the time no matter how many times you rotate around the object.
Answer by Eric5h5 · Dec 16, 2010 at 04:29 AM
This seems to only do between 0 and 179.9 degrees and still has the issue where if you go 180 on one axis and then 180 on another and start moving along the first axis again it does not reset back to zero and ins$$anonymous$$d starts at 180.
Answer by alireza khodakarami · May 16, 2011 at 02:17 PM
It's a known Isue from early days of 3D animation software development called "gimbal lock" the basic concept is when you rotate on one axis and then try another one, the order is important because sometimes the local rotational axis will be on top of each other and no matter which one you rotate, the object will rotate the same, so, different programs found different solutions for this problem, I think maybe the problem you are face to face is the approach of unity to solve gimbal lock.
Unity tends to use Quaternions to avoit gimbal lock- except, that quaternions don't really respond well to interpretation by real numbers! You might want to have a look at Atan2 or the like- they might have the solution for this one.