- Home /
Project a position onto a plane whose normal isn't at the origin.
I need to project an object's position onto a plane. Unity provides this method :
Vector3.ProjectOnPlane(Vector3 positionToProject, Vector3 normalOfPlane)
This method projects a Vector 3 onto a plane whose normal is at the origin. I have been trying to take the result from this and apply it to the position of another object, but every attempt so far has failed. Is there a way to project on to a plane defined by a normal that isn't on the origin?
Answer by asafsitner · Feb 18, 2017 at 04:42 AM
I know it's an old question, but as I ran into the same problem today, I'll answer:
Unity's built-in methods do not have an option to specify the plane's origin, however, it's possible to do so manually and use Plane.Raycast to find the intersection point, for example:
private void ProjectTransformOnPlane(Transform objectToProject, Vector3 planeOrigin, Vector3 planeNormal)
{
Plane projectionPlane = new Plane(planeNormal, planeOrigin);
float distanceToIntersection;
Ray intersectionRay = new Ray(objectToProject.position, objectToProject.forward);
if (projectionPlane.Raycast(intersectionRay, out distanceToIntersection))
{
objectToProject.position = objectToProject.position + objectToProject.forward * distanceToIntersection;
}
}
This principle can be modified and applied to different scenarios, but in each of them, we must have the origin and direction of the vector we'd like to project onto the plane.
In this case, I used the Transform's forward to find its direction, but any other vector can be used.
Answer by PraetorBlue · Feb 14, 2019 at 01:55 AM
Since you are working with Vectors and not Rays, it is safe to conceptualize all vectors as being "on the origin".
What the projection of the vector on the plane will do is give you the component of the vector which is parallel to the plane. You can then do with this projection whatever you want. Your question is a little unclear with regards to your use case, so it's hard to say much more than that.