# how to calculate the angle between two vectors?

i need to calculate the angle between two vectors. the result should be something between 180 and -180 or 0 and 360. i mean Vector3.Angle is not useful because the result is between 0 and 180.

Here is both an easily understandable and computationally fast method for getting an angle from -180 to +180.

``````// the vector that we want to measure an angle from
Vector3 referenceForward = /* some vector that is not Vector3.up */

// the vector perpendicular to referenceForward (90 degrees clockwise)
// (used to determine if angle is positive or negative)
Vector3 referenceRight= Vector3.Cross(Vector3.up, referenceForward);

// the vector of interest
Vector3 newDirection = /* some vector that we're interested in */

// Get the angle in degrees between 0 and 180
float angle = Vector3.Angle(newDirection, referenceForward);

// Determine if the degree value should be negative.  Here, a positive value
// from the dot product means that our vector is on the right of the reference vector
// whereas a negative value means we're on the left.
float sign = Mathf.Sign(Vector3.Dot(newDirection, referenceRight));

float finalAngle = sign * angle;
``````

You can switch the 1.0f and -1.0f around above if you want the left side to be positive and right side negative instead.

The only way I've found is to determine if the objects are left or right of each other then subtract from 360 accordingly. It isn't real clean, but it works fine.

This is my solution for getting the directed angle between 2D vectors :

``````float angle = Mathf.DeltaAngle(Mathf.Atan2(source.y, source.x) * Mathf.Rad2Deg,
``````

Everyone… I have wasted too much time on this, trying to calculate the angle and solve the BS… Apparently Quaternion.LookRotation, does this without having to work about correcting angles, or flipping! Amazing! Unity - Scripting API: Quaternion.LookRotation
Hope this helps:
private void SetRotationFromWorldPoint(Transform trans, Vector3 worldPoint)
{
Quaternion currentRotation = trans.rotation;
currentRotation = Quaternion.LookRotation(worldPoint);
trans.rotation = currentRotation;
}

I have a question on this topic and the answer above does not seem to address it. My problem is this:

I have two spheres where are moving along a path using iTween (they represent two points along a cylinder - the middle and the top). The paths that the spheres travel on was determined through motion capture data of a person swinging a bat like object. I am trying to have a cylinder object follow a path that essentially goes through both these points. The two spheres are essentially markers on the cylinder object.

Right now I have a line renderer object that is drawing a line between both spheres. I want to replace the line renderer with a solid cylinder. For example, if the bottom marker is at (0,0,0) and the top marker is at (0,1,0) I would like my cylinder object to be perfectly upright along the y-axis. Similarly, if the bottom marker is at (0,0,0) and the top marker is at (0,0,1) I’d like the cylinder object to lie horizontal along the z-axis.

My approach is to have the cylinder follow one of the marker’s path and find the angle made between both markers to have the cylinder object orient itself at that angle. This is proving to be quite difficult and I could use some help. Has anyone come across a similar problem?

Any help would be greatly appreciated. I’ve tried just about everything I could find to accomplish my goal but it just simply isn’t working.

I would also like to do this via a script component because the two markers (and their paths) will be changing based on motion capture data.

``````	public static float Angle360(Vector2 v1, Vector2 v2)