Flawed Mesh Manipulation Question!

I have the deforming mesh script curtesy of World of Zero (Deforming a Mesh at Runtime Based on Collisions with Unity - YouTube) and it seems to be pretty flawed. It doesn’t work when I have it above a certain y value or when it’s rotated. I’m looking to fix that but I suck at mesh manipulation.

Heres the deformable mesh script:

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

public class DeformableMesh : MonoBehaviour

    public float maximumDepression;
    public List<Vector3> originalVertices;
    public List<Vector3> modifiedVertices;

    private GeneratePlaneMesh plane;

    public void MeshRegenerated()
        plane = GetComponent<GeneratePlaneMesh>();
        originalVertices = plane.mesh.vertices.ToList();
        modifiedVertices = plane.mesh.vertices.ToList();
        Debug.Log("Mesh Regenerated");

    public void AddDepression(Vector3 depressionPoint, float radius)
        var worldPos4 = this.transform.worldToLocalMatrix * depressionPoint;
        var worldPos = new Vector3(worldPos4.x + transform.position.x, worldPos4.y, worldPos4.z + transform.position.z);
        for (int i = 0; i < modifiedVertices.Count; ++i)
            var distance = (worldPos - (modifiedVertices _+ Vector3.down * maximumDepression)).magnitude;_

//and if verticy height is higher than maximum height
if (distance < radius)
var newVert = originalVertices + Vector3.down * maximumDepression;
// times depression per hit instead of maximum depression

modifiedVertices.Insert(i, newVert);

Debug.Log(“Mesh Depressed”);
I will be forever grateful if someone helps out. I’ve never had a question answered on this stupid site but 10+ hours have gone into this.

There are a few things that can be addressed here:

First, by this use of a radius away from the point(s) of contact, if the modified vertices have already been shoved far enough away (down?), then their neighboring vertices would become hard to reach using that radius. You could dig an incredibly deep pit (theoretically) and not be able to lower adjacent vertices if the height difference is currently greater than the provided radius (for better or worse). Similarly, depressions made are a fixed depth, but that seems to be the result of the video cutting the process short and not working things through to completion.

Next, regardless of whether the depression is scaled by the distance from the point of contact (it’s currently not), an unnecessary square root calculation is making this whole process needlessly taxing.

// Untested, but should be a more straight-forward approach to this
// (Also, not having looked into how the video author generated the plane mesh,
// this assumes that its vertices are centered around or near a local (0, 0, 0) )
public void AddDepression(Vector3 depressionPoint, float radius)
	// Simplified converting contact point to local position
	Vector3 contactPoint = plane.transform.InverseTransformPoint(depressionPoint);
	for(int i = 0; i < originalVertices.Count; i++)
		float distance = (contactPoint - originalVertices*).sqrMagnitude;*
  •  if(distance <= radius)*
  •  {*
  •  	// Since changes are made locally to the mesh, this uses*
  •  	// Vector3.down rather than transform.down*

_ Vector3 newVertex = originalVertices + Vector3.down * maximumDepression;_

  •  	// Changed this because... why was it handled the way it was?*

_ modifiedVertices = newVertex;_
* }*
* }*
* plane.mesh.SetVertices(modifiedVertices);*

Unfortunately, this approach to handling ground deformation will always see significant performance hits as the vertex count/ground complexity increases. Ultimately, this leads most modern games to use a mainly-Shader-driven system for mostly-fixed deformations like snow.