Set plane verticies to match elevation of terrain.

Hi,

here is the code I’m currently using. It creates a plane, set the position above a terrain and then attempts to alter the vertices to match the elevation of the terrain. However the plane just continues to intersect a hill.

using System.Linq;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//using Data;

public class HouseSystem : MonoBehaviour
{
   private GameObject plane = null;
   public LayerMask groundLayer;
   
   void Awake() {
     
     plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
     //plane.transform.Rotate(new Vector3(90, 0, 0));
     
     MeshRenderer renderer = plane.GetComponent<MeshRenderer>();
     renderer.material.shader = Shader.Find ("Particles/Additive");
     Texture2D tex = new Texture2D(1, 1);
     tex.SetPixel(0, 0, Color.blue);
     tex.Apply();
     renderer.material.mainTexture = tex;
     renderer.material.color = Color.blue;
   }
   
   void Update () {
     //Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     //Debug.DrawRay (ray.origin, ray.direction * 10, Color.cyan);
     //RaycastHit hit;
     
     //if(Physics.Raycast(ray, out hit))
     //{
       if(plane != null) {
       
         plane.transform.position = new Vector3(183, 0, 179);
         
         GameObject go = plane;
         Mesh m = (Mesh)Instantiate(go.GetComponent<MeshFilter>().sharedMesh);
         
         Vector3[] verts = m.vertices;
         
         for(int i = 0; i < verts.Length; i++)
         {
           Vector3 castFrom = go.transform.TransformPoint(verts[i]);
           
         
             //Debug.DrawRay (ray.origin, ray.direction * 10, Color.cyan);
           verts[i] = go.transform.InverseTransformPoint(GetPosition(castFrom));
         }
         
         m.vertices = verts;
         m.RecalculateBounds();
         m.RecalculateNormals();
         m.Optimize();
       }
     
   }
   
   public Vector3 GetPosition(Vector3 _position){
     RaycastHit hit;
     
     if(Physics.Raycast(_position, -Vector3.down, out hit, Mathf.Infinity, groundLayer)){
       _position.y = hit.point.y + 0.1f;
     }else
       _position.y = transform.position.y + 0.1f;
     return _position;
   }
}

Got it working…

go.GetComponent<MeshFilter>().sharedMesh = m;

I guess it helps if I assign the modified mesh back to the meshfilter. Doh >_<