Skip to content

Instantly share code, notes, and snippets.

@ditzel
Last active June 20, 2023 08:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ditzel/f5179df11b54957d8b9f1a17f14ff673 to your computer and use it in GitHub Desktop.
Save ditzel/f5179df11b54957d8b9f1a17f14ff673 to your computer and use it in GitHub Desktop.
Unity Math
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