- Home /
I don't understand Vector3.ProjectOnPlane...
I can't seem to wrap my head around what vectors I have to put into ProjectOnPlane. To try to understand it, I've created two boxes with cast rays directly below them.
So I have the plane normal (as shown as the green line) and the points that hit the plane. What values do I need to put into Vector3.ProjectOnPlane(vector, planeNormal) to get the intended result?
Answer by Captain_Pineapple · Feb 12, 2019 at 10:12 PM
Hey there,
b has to be the planes normal. a is the vector that will be projected on the plane defined by the normal b. you should get the normal from the downward raycast hits hit normal.
Does this clear things up?
So if the green line in the image is the normal, what would "a" be?
I'm afraid to answer that you have to provide some information on what you want to do. If you want for example the "slope-vector" in character forward direction then a is transform.forward.
But as i said it depends on your usecase. Let us know some more on what you plan to do with the result.
Sorry for all the confusion, I'm a tad new to unity. I have set up a scene like the image, and I'd like to project a vector on the plane using the two rays that are being cast on it by the boxes. So what I know is
projectedVector = Vector3.ProjectOnPlane(a, rayB.normal);
But I don't know what to put in "a".
Answer by Bunny83 · Feb 12, 2019 at 07:58 PM
ProjectOnPlane will project a direction vector onto the plane, not positions. Just think about removing any offset along the given plane normal from the vector. So if you move the origin of the vector somewhere onto the plane, the resulting vector will lay completely on that plane.
ProjectOnPlane is literally just defined as:
public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal)
{
return vector - Vector3.Project(vector, planeNormal);
}
This is what i just mentioned. Project will project the vector onto the normal vector, returning only the part of the vector along the normal. This amount is subtracted from the original vector which results in a vector that is parallel to the plane surface.
Note that from your image it's not clear what your "intended result" is. Maybe you wanted to actually cast a ray onto a plane? In that case you may want to use Unity's Plane struct and its Raycast method.
Thanks for the reply. The goal of what I'm trying to do it to get a vector to project along the plane, I'm using this to try to get smooth slope movement for my character controller so I can move it parallel to the surface of which the character is moving on. I do understand what Project and ProjectOnPlane do, I'm just confused as to what vectors are put into the method.
projectedVector = Vector3.ProjectOnPlane(a, b);
I'd just like to know what to replace "a" and "b" with.