Moving certain vertices to a position

I have a game where I have a mesh terrain and a building system. When you build a building and place it, I would like the mesh to fit well onto the building.

Is it possible to move the vertices of a plane (terrain), only below a cube (building).

So this-

To this-

For modifying meshes, see the examples in the scripting reference: [Mesh][1]. To test if a vertex is below an object and get its distance to it, you can use [Collider.Raycast][2] with an upwards ray starting at the vertex. Here is a basic example:

using UnityEngine;

public class RaiseTerrain : MonoBehaviour {
	public Collider building;

	void Start() {
		var mesh = GetComponent<MeshFilter>().mesh;
		var vertices = mesh.vertices;

		for (int i = 0; i < vertices.Length; i++) {
			var vertex = transform.TransformPoint(vertices*);*
  •  	var ray = new Ray(vertex, Vector3.up);*
    
  •  	if (building.Raycast(ray, out var hit, Mathf.Infinity)) {*
    
  •  		vertex.y += hit.distance;*
    

_ vertices = transform.InverseTransformPoint(vertex);_
* }*
* }*

* mesh.SetVertices(vertices);*
* mesh.RecalculateBounds();*
* mesh.RecalculateNormals();*
* }*
}
_[1]: https://docs.unity3d.com/ScriptReference/Mesh.html*_
_
[2]: https://docs.unity3d.com/ScriptReference/Collider.Raycast.html*_