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;
[RequireComponent(typeof(GeneratePlaneMesh))]
public class DeformableMesh : MonoBehaviour
{
public float maximumDepression;
public List<Vector3> originalVertices;
public List<Vector3> modifiedVertices;
private GeneratePlaneMesh plane;
public void MeshRegenerated()
{
plane = GetComponent<GeneratePlaneMesh>();
plane.mesh.MarkDynamic();
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);
Debug.Log(worldPos);
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.RemoveAt(i);
modifiedVertices.Insert(i, newVert);
}
}
plane.mesh.SetVertices(modifiedVertices);
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.