How Vector.Angle works internally in Unity?

Is it using the way this page shows?

Vector3.Angle does this:

public static float Angle(Vector3 from, Vector3 to)
{
    return Mathf.Acos(Mathf.Clamp(Vector3.Dot(from.normalized, to.normalized), -1f, 1f)) * 57.29578f;
}

To break it further down:

  • Both vectors are normalized first. So their lengths is 1.0.
  • The dot product between two vectors is simply a.x*b.x + a.y*b.y + a.z*b.z This gives you a single float value.
  • The dot product is also defined as |a| * |b| * cos(alpha) where |a| is the length of vector a and “alpha” the angle between the two vectors.
  • Since both vectors have a length of 1.0 the result of the dot product is just the cosine of the angle.
  • Mathf.Clamp simply keeps the value in the range -1 to 1 to avoid errors as Mathf.Acos only accepts values in that range.
  • Mathf.Acos calculates the “inverse” cosine in radians.
  • The magic number 57.29578f is just 180f / PI to convert radians to degree

Keep in mind that Vector3.Angle returns an angle in the range 0° to 180° as 180 is the largest possible angle between two vectors.

Vector3.Angle returns the angle in degrees between two input vectors.

About the page you share. If you represent the triangle’s sides with vectors, with this method you can get the angles on the corners.