Does Unity already have a method to project a vec3 onto a plane like below? If not, I’m curious where they draw the line on what to include in the API and what to leave out.
90% of the time they’ve already got a method for what I’m trying to accomplish, but occasionally I run across things I would consider “missing” or really helpful for other game developers new to 3d. Often times I’ll find an insightful solution simply by browsing the documentation when a particular helper function catches my eye (for example Transform/Inverse TransformPoint, Vector3.RotateTowards or Quaternion.LookRotation).
I guess this is more a question to satisfy my personal curiosity, but I’d love to hear from the community or someone who works at Unity. I realize everyone has their own opinion on what they think should be included in an API or math library, but I was wondering if there was more to it considering the Unity APIs are already fairly large.
Cheers! =)
public static class Vector3Extensions {
public static Vector3 ProjectOnPlane(this Vector3 v, Vector3 n) {
n = n.normalized;
float d = Vector3.Dot(v, n);
return v - d * n;
}
}
A few other goodies we use (same for SphereCollider):
public static class BoxColliderExtensions {
public static bool IsPointInBox(this BoxCollider box, Vector3 point) {
point = box.transform.InverseTransformPoint(point) - box.center;
Vector3 half = 0.5f * box.size;
return point.x < half.x && point.x > -half.x &&
point.y < half.y && point.y > -half.y &&
point.z < half.z && point.z > -half.z;
}
public static Vector3 ClosestPointInBox(this BoxCollider box, Vector3 point) {
point = box.transform.InverseTransformPoint(point) - box.center;
Vector3 half = 0.5f * box.size;
point.x = Mathf.Clamp(point.x, -half.x, half.x);
point.y = Mathf.Clamp(point.y, -half.y, half.y);
point.z = Mathf.Clamp(point.z, -half.z, half.z);
return box.transform.TransformPoint(point + box.center);
}
}