Skip to content

Instantly share code, notes, and snippets.

@belzecue
Forked from josephbk117/SurfaceImitator.cs
Created July 21, 2018 16:35
Show Gist options
  • Save belzecue/4787f1a435b6810100218ce8ec0236ee to your computer and use it in GitHub Desktop.
Save belzecue/4787f1a435b6810100218ce8ec0236ee to your computer and use it in GitHub Desktop.
depth surface generated with the help of surface generator script
using UnityEngine;
public class SurfaceImitator : MonoBehaviour
{
public SurfaceGenerator reference;
public bool generateCollider = true;
private Mesh mesh;
private Vector3[] vertices;
void Start()
{
mesh = GetComponent<MeshFilter>().mesh;
vertices = mesh.vertices;
}
void LateUpdate()
{
vertices = mesh.vertices;
int counter = 0;
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
vertices[counter].y = reference.vertices[i].z;
counter++;
}
}
mesh.vertices = vertices;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
if (generateCollider)
{
Destroy(GetComponent<MeshCollider>());
MeshCollider colliderComponent = gameObject.AddComponent<MeshCollider>();
colliderComponent.sharedMesh = null;
colliderComponent.sharedMesh = mesh;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment