Vector3.SignedAngle seemingly avoids values close to 0 and +-180

Hello!

I’m using Vector3.SignedAngle to calculate an approximate yaw of an object, relative to world z axis:

var handleYaw = Vector3.SignedAngle(Vector3.forward, grabHandle.transform.forward, grabHandle.transform.up);

I’ve been logging the value of handleYaw while rotating the grabHandle object.

While grabHandle’s up vector mostly (but not perfectly) aligns with World up (Vector3.up), the values get weird when its forward vector starts aligning with axis created by Vector3.forward.

Instead of the angle getting close to 0 or ±180, it simply flips the sign. So let’s say I have an angle of 10, and I move 1 degree counter-clockwise. Instead of the new angle being 9, it’s something like -9.5. Or if it’s 173, and I move 1 degree clockwise it’s suddenly -174.

This only happens as the angle gets close to 0 or ±180, it doesn’t happen otherwise. Does anyone know what’s going on here?

Is it some sort of confusion with gimbal lock as two of the grabHandle’s axes align with World Up and World Forward? Or maybe something entirely else?

Any help towards fixing the issue would be greatly appreciated.

Well, your issue is that when your two vectors are relatively close to each other the 2d plane that the two vectors describe can easily flip around to the other side. SignedAngle still calculates the actual angle between the two vectors, not the angle in a certain plane. The given axis vector is only used to determine the sign of the angle. So imagine your forward vector is pointing exactly forward but is tilted upwards by 9°. Then SignedAngle will still give you an angle of 9°. However the normal of the plane your two vectors lie in points to the right. Now if you rotate slightly to the right that normal would move downward a little bit. Now that normal points in the opposite direction that your reference axis and the angle flips sides.

What you probably want is something like that:

var fwd = grabHandle.transform.forward;
fwd.y = 0;
var handleYaw = Vector3.SignedAngle(Vector3.forward, fwd, Vector3.up);

This gives you the signed angle in the horizontal worldspace “x-z” plane.

Though in this case it might be simpler to just use Mathf.Atan2 with the x and z components of your forward vector.

var fwd = grabHandle.transform.forward;
var yaw = Mathf.Atan2(fwd.x, fwd.z) * Mathf.Rad2Deg;

Note that depending on where you want 0° and in which way the angle should increase you might have to add an offset to the result and wrap it accordingly.

ps: If you also looking for the pitch, this can simply be taken from the y component

var pitch = Mathf.Asin(grabHandle.transform.forward.y) * Mathf.Rad2Deg;

while this would probably work fine in most cases, it’s recommended to wrap the y value in a Mathf.Clamp(y, -1f, 1f) because Asin must not be fed with a value any larger than 1 or smaller than -1. If you do you get back a NaN (nor a number). Unity does the same thing in the Angle method

if ((angleToTarget > 0 && angleToTarget <= 180) || (angleToTarget < -180 && angleToTarget >= -360))
{}

simplest solution