Setting the terrain height to match a gameobject by vertices?

I’m generally aware that there are resources out there already for setting the height for the terrain but as ever I seem to have a habit of picking suddenly complicated maths problems that usually have links to extremely outdated videos or documentation. What I want to do is make it so that when a gameobject is instantiate in runtime the terrain flattens itself and the height moves to match the gameobject’s surface.

Now doing this with a relatively flat surface is pretty straightforward and predictable, but that’s the problem of course, terrain is never usually like that. I can see this being a pretty big problem with cliffs when you have the gameobject half hanging off a cliff and so on and I need to find a way to detect that. The most obvious solution seems to be raycasts but then you have the issue with what would you raycast to? The vertices will work I’m sure but if you don’t have the vertices matching up with the raycasts at a perfect angle then the terrain will simply blob onto the gameobject in order to match up with it.

Would appreciate a bit of insight into this problem and maybe some more up to date resources to look at, just posting my thoughts. I wonder if I should have a look at mesh deformation generally to get ideas.

I got up-to-date resources for sure!

https://www.youtube.com/watch?v=FykNmJ3NIpI

Full source code at:

https://gist.github.com/kurtdekker/f9e7b0bdf4b2f9c0455a99f7f0f4a77a

There’s a lot of steps and stages, but they’re all pretty well documented right in the code.