- Home /
Is there a way to project a vector3 onto a plane?
I'm aware of being able to do so onto another vector, but I need to be able to do functionally the same along 2 vectors, or a plane, decided by an object other than the one I want to move along the plane. I'm effectively trying to flatten vectors onto a plane.
As a practical example, I have a player in first person view who wants to climb a wall. The direction of his camera is what he uses to decide his direction, but his actual movement is limited to be along a plane of the climbable surface.
Answer by robertbu · Nov 11, 2014 at 08:12 PM
Here are two functions. The ProjectVectorOnPlane() is untested and uses Vector3.zero for the plane point.
function ProjectVectorOnPlane(planeNormal : Vector3, v : Vector3 ) : Vector3 {
planeNormal.Normalize();
var distance = -Vector3.Dot(planeNormal.normalized, v);
return v + planeNormal * distance;
}
function ProjectPointOnPlane(planeNormal : Vector3 , planePoint : Vector3 , point : Vector3 ) : Vector3 {
planeNormal.Normalize();
var distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint));
return point + planeNormal * distance;
}
Answer by gw1108 · Dec 14, 2016 at 06:54 AM
You can just use Vector3.ProjectOnPlane(Vector3 vector, Vector3 planeNormal);
More info here: https://docs.unity3d.com/ScriptReference/Vector3.ProjectOnPlane.html