# How to get the positive or negative angle between objects?

I need to decide if I want something to swivel left or right towards my target. Currently, angle only seems to return positive values. I need to find out, if two objects are level, if I need to add angles to the y rotation of my object, or subtract angles in order for it to eventually face towards my target.

Is there a straightforward function in Unity I've overlooked?

You can use Mathf.Atan2 to compute the angle between 2D vectors. This only works for 2d rotations though, however often that's all you need even in a 3d game. Eg:

``````var localTarget = transform.InverseTransformPoint(target.position);
var targetAngle = Mathf.Atan2(localTarget.x, localTarget.z);

``````

However, the "angle" will be in radians, not degrees, so you most probably want to convert it to degrees before using it, like this:

``````var targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;

``````

This should give you the target angle in the range -180 to 180 degrees.

You can test to see if the angle returned it more than 180 and then reverse the rotation your self (-360).

Something like:

``````function getAngle(){
// angle code
if (Angle > 180) Angle -= 360;
return Angle;
}

``````angle_diff = asin(cross(v1, v2).len())