I wrote the code below to get Yaw.
However, this code only gets -180 ~ 180 Yaw value.
Let’s try to illustrate what I want.
I want the value to be 720 if the object rotate twice in the same direction.
public float GetYaw()
{
var down = -transform.up;
down.y = 0;
Yaw = Vector3.Angle(-Vector3.right, down) * Mathf.Sign(transform.up.z);
return Yaw;
}
Let’s be clear about one point in your question. There is no information that comes from vectors which can tell you how many times a rotation has been performed. If an object rotated 765 degrees clockwise in the xz plane, the only information you’ll get back from the resulting information in a Transform will be that the object rotated 45 degrees clockwise, because that is all the information that remains.
Vector3.Angle returns the angle between two vectors, without regard to any axis. If you define yaw as a rotation in the xz plane, there is no relevance to down or up. I would infer that a relationship to the world ‘negative right’ direction and the transform’s ‘down’ direction is more that of a pitch than a yaw, but Vector3.Angle is going to return the angle between ‘negative right’ and wherever ‘down’ is pointing, which won’t be relative to an axis. Imagine pointing your left hand straight out left (that’s what negative right means, no?), then point your right hand ahead of you, but down from straight out 45 degrees. Now, in yaw (the xz plane), your arms are 90 degrees apart, but the angle between your hands will be somewhere less than 90 but greater than 45 degrees, in a plane at an angle not aligned with a reference to a yaw.
You can get the rotation of an object (in the range of -180 to 180 degrees) using Mathf.Atan2, providing the coordinates you get from transform.forward ( x and z, ignoring y ). The return value is in radians which you can convert by multiplying the return by Mathf.Rad2Deg. Note, however, that the standard orientation may require adjustment to your expectations, because in math zero degrees (or zero radians) is on the X axis (to the right), and positive rotation is counterclockwise. Once you adjust that for your expectation, you’ll get the angle in the xz plane the object is facing.
I omited some explanation. In my case, transform.up points his backside. So down means forward.
then I used “down.y =0” to bring vectors to xz plane.
So I can already get yaw based on left(-Vector3.right) in xz plane.
but It’s range is -180~180.
so I want to get more big size of range.