I would have though there would be a function within the Vector3 class or similar to convert a position vector into the equivalent rotation vector in eulerangles… but I can’t seem to find anything.
e.g. if I have worked out my heading Vector3 is (-5,0,-5);
Vector3.Convert(heading : Vector3) would return a Vector3 (0,315,0) which could then set the rotation.
I’m trying to avoid getting into quaternions since the Unity manual says “You shouldn’t need to use these”.
Let me know if I’ve missed an obvious way to do this or even if there are any good web resources that explain the 3d math better in Unity.
There wouldn’t be such a function, because there’s no meaningful way to convert a position to an orientation.
So it’s actually a direction vector you want to convert? In that case, there actually is such a function: Quaternion.LookRotation(). (Note that there’s no one-to-one mapping from direction vectors to orientations; to work around that, LookRotation() has an optional second argument that allows you to fully specify the orientation, provided the input is valid.)
If it says that somewhere, I wouldn’t make too much of it. Sometimes it’s easier to work directly with the ‘rotation’ fields than with the ‘Euler angle’ fields, and sometimes it’s the other way around, so just use whatever representation is most appropriate for the task at hand.
and many combinations of the numbers 0.7 1 and 0.0 which clearly isnt euler angles for vector3 up right and fowards. so the function you describe doesnt return eulers of a transformdirection which is what i want.
Yes, of course you can convert vectors into Angles, you just need to specify your frame of reference. That is, what vector you are comparing the position/direction vector against. Whats the frame of reference? It might be world coordinates north (direction), it might be world coordinates centre (position), it also might be local coordinates like drift/sideslip vector compared to forward vector.
Vector3.Angle will return the unsigned angle between 2 vectors.
You could use Atan2 to get a signed angle.
Just like Jessie says be careful what you use angles for, they are great because they are simple for some things (eg rotation in 1 axis), but are considered naughty and stupidly difficult for others (eg managing orientation in multiple axis).
You can substitute your own Vector3 in place of rigidbody.velocity, such as one that represents the direction an object is facing.
It returns 0 as the heading if there is no velocity as Quaternion.LookRotation() doesn’t like you passing it Vector3s that are zero.
This returns the “compass” heading relative to world coordinates (rotation around Vector.up, just like a real compass relative to the imaginary flat plane of a map).
Or another way to get the quick heading (direction it’s facing regardless of movement) of an object relative to the world is just to grab this value:
heading = rigidbody.transform.eulerAngles.y;
I’m totally new to Unity so there might be a more effective solution, but these worked for me!