Get a vertical angle to another game object (ignoring direction)

Hi All,

Im trying to get a vertical angle to another game object which ignores the direction the object is facing, its for a steering behaviour I am writing.

I have been working with the following code:

float angle = Vector3.Angle (transform.up, targetDirection);
float vangle = angle * Mathf.Sign (Vector3.Cross (transform.up, targetDirection).x);

which I think gives me a vertical angle, however this varies depending on wether the object is in front or behind. Ideally I just want to know what the angle is regardless of wether it is infront or behind the game object making the measurement. with 0 being horizontal -90 being directly below and 90 being directly above.

Is this possible? could anyone point me in the right direction? Cheers!

1 Answer

1

Here’s one way: compare the target direction to the target direction ignoring the Y component. Your sign is the sign of dot product of the actual target direction and global up.

Vector3 targetHoriz = targetDirection;
targetHoriz.y = 0;
float angle = Vector3.Angle(targetDirection, targetHoriz) * Mathf.Sign(Vector3.Dot(targetDirection, Vector3.up));