Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity3d 5 WavePro Dynamic MeshCollider

Tags:

c#

unity3d

Im using Water4Advance to simulate an ocean waves in Unity3d 5.0. I have my plane displaced in Runtime with Gerstner Displace. I see how the mesh is deformed and i add a MeshCollider to it and i like to refresh this collider mesh in runtime. I was working this on Unity 4.6 with this script:

MeshCollider collider = GetComponent<MeshCollider>();
Mesh mesh = GetComponent<MeshFilter>().mesh;
collider.sharedMesh = null;
collider.sharedMesh = mesh;

But now i only got the flat original plane prefab. enter image description here

How can i update this MeshCollider with the displaced mesh?

like image 271
joreldraw Avatar asked May 15 '15 09:05

joreldraw


1 Answers

Try this:

Mesh myMesh = this.GetComponent<MeshFilter>().mesh;
DestroyImmediate(this.GetComponent<MeshCollider>());
var collider = this.AddComponent<MeshCollider>();
collider.sharedMesh = myMesh;

From here:

http://answers.unity3d.com/questions/446910/changing-mesh-collider-at-run-time.html

like image 164
scotru Avatar answered Sep 19 '22 21:09

scotru