# Calculate the angle of rotation between two vectors relative to the first vector

I'm trying to get the angle between two 2D vectors relative to the first vector input. Every method I've tried has given me a value between 0-180 degrees but doesn't tell me whether it was a clockwise or counter-clockwise rotation.

For example: If I hold my forward vector in a variable and then turn counter-clockwise 45 degrees and use that as my second forward vector, I'm looking for -45 or 315 degrees as the output, not 45.

float AngleBetween(Vector2 a, Vector2 b) {
// Get a vector rotated 90 degrees from a.
Vector2 perpendicular = new Vector2(a.y, -a.x);

// Compute a scaled projection of b onto the original and rotated version.
float x = Vector2.Dot(a, b);
float y = Vector2.Dot(perpendicular, b);

// Treat these as a point in a coordinate system where a is the x axis,
// and perpendicular is the y axis, and get the polar angle of that point.
return Mathf.Atan2(y, x);
}


There is a solution for unity (Maybe there is a better solution, but it works):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
void Start()
{
var v1 = new Vector3(1, 1, 0);//45
var v2 = new Vector3(1, 0, 0);//0

Debug.Log(GetDegree(v1, v2));
Debug.Log(GetDegree(v2, v1));
}

private float GetDegree(Vector3 from, Vector3 to)
{
var rot = Quaternion.FromToRotation(from, to);
var angle = 0.0f;
var axis = Vector3.zero;
rot.ToAngleAxis(out angle, out axis);

if (Vector3.forward == axis)
{
return angle;
}
else
{
return angle - 360f;
}
}
}


Output:

-315
44.99999