Last active
June 20, 2023 08:30
-
-
Save ditzel/f5179df11b54957d8b9f1a17f14ff673 to your computer and use it in GitHub Desktop.
Unity Math
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
static class Math3D | |
{ | |
private static System.Random rand = new System.Random(); | |
public static float DistanceToLine(Ray ray, Vector3 point) | |
{ | |
//see:http://answers.unity3d.com/questions/62644/distance-between-a-ray-and-a-point.html | |
return Vector3.Cross(ray.direction, point - ray.origin).magnitude; | |
} | |
public static Vector3 ClosestPointInLine(Ray ray, Vector3 point) | |
{ | |
return ray.origin + ray.direction * Vector3.Dot(ray.direction, point - ray.origin); | |
} | |
public static Vector3 toGround(Vector3 inVector) | |
{ | |
var outVector = inVector; | |
outVector.y = 0; | |
return outVector; | |
} | |
public static float CWAngle(Vector2 from, Vector2 to) | |
{ | |
float output = Vector2.Angle(from, to); | |
if (Vector3.Cross(from, to).z < 0) | |
output = 360f - output; | |
return output; | |
} | |
public static float newRandomNumber(float min, float max) | |
{ | |
return (min + (float)rand.NextDouble() * (max - min)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment