Created
September 15, 2016 04:04
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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