- Home /
find angle between gameobject and world axis?
this didn't work and i dont know why:"
Vector3.Angle(go.transform.eulerAngles ,transform.forward);
the result stays stuck at 90 for any go.rotation
Answer by Owen-Reynolds · Apr 06, 2014 at 06:19 AM
You wrote world axis. So don't you think the equation should have that? For example, X is Vector3.right
(or new Vector3(50,0,0)
, etc... anything with positive X and 0 for Y and Z.)
transform.forward
is the direction the object is facing (the blue arrow, which is where it "counts as" facing. If it isn't, most people fix it.)
So, to get the angle between object facing and right facing, use Vector3.Angle(transform.forward, Vector3.right)
. The angle is if you put the starting ends of both arrows together. 0 means you're facing right. 10 means you facing almost right -- titled 10 degrees up or down from that. If you need to know which one (often you don't) you can search around here for that trick. It's also 3D, so 10 could mean you're looking mostly right, but 10 degrees up.
Answer by supernat · Apr 06, 2014 at 06:18 AM
eulerAngles is not a directional vector, so this won't work. Vector3.Angle is used to find an angle between two directional vectors, i.e. vectors that when normalized would point in the direction something is facing. The vectors don't necessarily have to be connected in any way, but usually it makes more sense to consider at least one of the ends points to reside at the same position. There are cases where you might just want the angular difference between two headings of two different objects.
I'm not sure exactly what you mean by finding the angle between the game object and the world axis. If you mean finding the game object's angle relative to the world axis, that's just eulerAngles. If you wanted the game object's angles relative to its parent, it would be localEulerAngles.
it s for a spacecraft turning towards the cursor position and flying towards Z axis, i wanted to clamp the rotation so that it never goes more than 45 degrees away from z axis.
so i just wanted the vector of the spacecraft and of the world z axis, and for some reason it's beyond me!
the solution is var deviation = Vector3.Angle(transform.forward, Vector3.forward);
Thanks!!!
I gotcha, yes I misunderstood, I probably would have worded the question in the same manner, so no worries. I'm sure you don't really care now that it's working with Angle(), but what that method is doing is computing the dot product between two vectors, then dividing that dot product by the magnitude of both vectors, then taking the acos() of the result to get your angle.
The magnitude part is a problem if you are looking for optimized code, because it uses the expensive sqrt operation twice. In most cases this won't be an issue (on mobile or consoles maybe a problem), but if you're trying to squeeze every bit of efficiency out, you can compute the dot product yourself, then just take the acos() of the dot product, because in your case Vector3.forward and transform.forward both have magnitudes of 1.0 (they're unit vectors). In fact, Vector3.forward never changes, and two components are 0, so you can actually get the angle by just saying acos(Vector3.forward.z*transform.forward.z) assu$$anonymous$$g the Vector3.forward value is 0,0,1 (and even this can be further reduced to just transform.forward.z). Just for fun. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613142046im_/https://answers.unity.com/themes/thub/images/avi.jpg)