- Home /
Find height above a mesh without raycasting (for bobbing a boat).
Hi guys. I don't like the indie water plane that comes with Unity, so I though I'd improve it. I've written a script that periodically uses the Perlin noise generator to generate new vertical positions for the vertices in the water plane mesh, then Lerps the vertices over time to make the water move up and down like waves. It works really well (well, I'm happy with it), but now I want to put boats on my newly moving water. I thought to start with I could just use a raycast on the boats going straight down, and use the raycast.hit to create a new position. This doesn't work though, since I've disabled the collider for the water, and anyhow the collider is fixed and doesn't move with the surface of the waves. What I really need is one of two things. Either:
1) find out how far above the surface of the water the boat is, using some other method
or
2) find a way to sample the Vector3 where a point on the boat intersects the mesh of the plane and use that to change the position of the boat.
Or some third thing I can't think of!
Can anybody help me please? Any help greatly appreciated, thankyou.
Answer by blueLED · Dec 11, 2014 at 03:19 AM
It seems to me that if you're generating the "new vertical positions for the vertices in the water plane mesh", then you know where the boat should be based on that, no?
That is to say, if you feed your noise generator an X and Z value, it can tell you the Y value? Then apply that to the boat.
Just a guess.
Hi, thanks for the quick reply. I do know where each of the vertices are on the Y axis, but the mesh is quite big (that is, large gaps between the vertices).
Since there's a large gap, one vert might be quite high, and one next to it low. This would give a steep plane, and depending on how close to either vert the boat is, it could be anywhere along that line from low to high.
I want a boat that looks like it's following the water, so following where it intersects the plane (regardless of how close it is to any verts).
Sorry, that was quite hard to explain clearly. Do you see what I'm trying to say?
Perhaps increase the size of the noise generated, so that the space between vertices can be sampled for height too. Then the boat would be closer to where it needs to be. Or maybe take and average of the 3 closest verts, but how you do that, I have no idea :P
Your answer
![](https://koobas.hobune.stream/wayback/20220613173743im_/https://answers.unity.com/themes/thub/images/avi.jpg)