So I have this terrain with a perpendicular plane setup, now I want to move the vertices of the plane to their closest resp. vertices on the terrain. I made a drawing to illustrate my thoughts. Final result being the terrain will look like it has some thickness.

i also need the projection of the point on the terrain

I found this code on a previous thread :

    float distance = PointToPlaneDistance(smallObj.transform.position, wall.transform.position, wallNormal);

    private float PointToPlaneDistance(Vector3 pointPosition, Vector3 planePosition, Vector3 planeNormal)
        float sb, sn, sd;

        sn = -Vector3.Dot(planeNormal, (pointPosition - planePosition));
        sd = Vector3.Dot(planeNormal, planeNormal);
        sb = sn / sd;

        Vector3 result = pointPosition + sb * planeNormal;
        return Vector3.Distance(pointPosition, result);

The result vector here is the closest point

But what is the plane normal? Unity has a built in mesh.normals which gives all the surface normals for the terrain. Which one should I use here?


1 Answer 1


Since your plane is perpendicular, you wouldn't use the normals. Just like your second drawing in your album:

enter image description here

Just ignore the Y and Z axis, of the terrain. For each point you want to match on the terrain, take the X axis value, and match that value on your perpendicular plane.

For example, if the point is at (1,3,0), just take the point (1,planeTop,0) on your plane.

Your terrain even appears to be a regular grid, meaning you can use a simple loop to iterate through the terrain vertices and match them with your plane vertices.

As for the projection, you don't have a flat plane, which is what that code is for. You have an uneven mesh. Using the normals will not help you. Just take the four values that make up the corners of the grid square you're directly above, then use linear interpolation to find the y value of the mesh at that point.

See this answer for more information.

  • \$\begingroup\$ i should have mentioned it before the plane has to move back and forth and fit to the terrain accordingly like this (imgur.com/8La7bMa), so i am raycasting from each vertex on the top of my plane and get the point it is hitting on the terrain. \$\endgroup\$ Commented Feb 22, 2015 at 20:59
  • \$\begingroup\$ That only changes the Z coordinate. So, in my example, in your loop, iterating over x: (x,planeTop,zDepth). It's still just one loop. \$\endgroup\$
    – House
    Commented Feb 22, 2015 at 21:03
  • \$\begingroup\$ this is the code for it \$\endgroup\$ Commented Feb 22, 2015 at 21:06
  • \$\begingroup\$ i am converting the vertices to world coordinates before raycasting and replacing the y coordinate with hit.y, finally converting back to the local space \$\endgroup\$ Commented Feb 22, 2015 at 21:07
  • \$\begingroup\$ Yep. Sounds overly complicated. Do you understand my answer? \$\endgroup\$
    – House
    Commented Feb 22, 2015 at 21:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .