- Home /
Mesh collider with a mesh created by code returns incorrect results.
So I need to create a plane in real time based on location of 4 different points as the vertices. Here's my script:
public class MeshTest : MonoBehaviour {
public GameObject[] Points;
public Material mat;
void Start() {
GameObject plane = new GameObject("Plane");
MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
renderer.material = mat;
Mesh m = new Mesh();
m.name = "ScriptedMesh";
m.vertices = new Vector3[] {
Points[0].transform.position,
Points[1].transform.position,
Points[2].transform.position,
Points[3].transform.position,
};
m.uv = new Vector2[] {
new Vector2 (0, 0),
new Vector2 (0, 1),
new Vector2 (1, 1),
new Vector2 (1, 0)
};
m.triangles = new int[] { 0, 1, 2, 0, 2, 3};
m.RecalculateNormals();
meshFilter.mesh = m;
MeshCollider collider = plane.AddComponent(typeof(MeshCollider)) as MeshCollider;
collider.convex = true;
}
}
Creating the mesh works fine, on start I get a correctly rendered plane spanning between the Points game objects, only the mesh collider doesn't work.
When the object is created, it's collider is just a simple 3D box. Changing the vertices positions doesn't do anything, neither does changing the order of commands, it seems like the component just doesn't accept this mesh although in the inspector it looks like it was assigned correctly.
Am I doing something wrong?
Answer by IgorAherne · Nov 01, 2015 at 11:37 PM
I remember having the same problem, during custom terrain generation.
As far as I remember, there isn't a function which would update the mesh collider. However, I use to solve it with either:
re-setting the collider.sharedMesh to the current shared mesh of your MeshFilter,
or disabling / enabling the collider (which forced it to update itself). One of those two things
Answer by Dzejkob1218 · Nov 02, 2015 at 02:46 PM
Allright, I've got this.
If anyone is having the same problem, here's my solution;
Mesh collider component doesn't work with 2D planes, you can try this on the generic plane
If you want to create planes with colliders (for example for terrain) you need to make a second set of vertices, move them down just a fraction of a unit and create a 3D shape, mesh collider should accept it.
This may be a little harder on performance since there are many more faces, but it works.
If you go to "GameObject > Create > Plane" (Or a quad) you'll see that it creates a plane with a mesh collider attached, so it is definitely possible to have a mesh collider with a 2D plane. Did you try my answer below?
Answer by Arycama · Nov 02, 2015 at 09:28 AM
After you add a mesh collider component on line 28, you need to tell the mesh collider what mesh to actually use. Adding the following line after line 28 should do the trick:
collider.sharedMesh = m;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why is my vertex array empty using GetComponent().mesh.vertices? 3 Answers
Pushing a mesh with mesh collider 1 Answer