Skip to content

Instantly share code, notes, and snippets.

@kimsama
Created September 15, 2016 04:04
Show Gist options
  • Select an option

  • Save kimsama/5cf52aa37568565ee3934d194598d34e to your computer and use it in GitHub Desktop.

Select an option

Save kimsama/5cf52aa37568565ee3934d194598d34e to your computer and use it in GitHub Desktop.
A way to calculate the angle around an axis (make it turn around its head y axis).
public static float AngleAroundAxis (Vector3 dirA, Vector3 dirB, Vector3 axis)
{
// Project A and B onto the plane orthogonal target axis
dirA = dirA - Vector3.Project (dirA, axis);
dirB = dirB - Vector3.Project (dirB, axis);
// Find (positive) angle between A and B
float angle = Vector3.Angle (dirA, dirB);
// Return angle multiplied with 1 or -1
return angle * (Vector3.Dot (axis, Vector3.Cross (dirA, dirB)) < 0 ? -1 : 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment