- Home /
MeshCollider has different shape than MeshFilter
Hello,
I am generating mesh and then I am assigning it to MeshCollider's sharedMesh. My problem is showed right on the attached picture. The shape of purple object is exactly what I wanted. However mesh collider has a different shape, and I expected it to have the same shape as object has.
This is the code of generating:
public class MeshGeneratortTest : MonoBehaviour {
public int width = 10;
void Start() {
gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
gameObject.AddComponent<Rigidbody>();
Mesh mesh = GetComponent<MeshFilter>().mesh;
mesh.Clear();
mesh.vertices = new Vector3[] {new Vector3(0, 0, 0), new Vector3(0, 0, width), new Vector3(20, 2, 0), new Vector3(20, 2, width),
new Vector3(34, 11, 0), new Vector3(34, 11, width) };
mesh.uv = new Vector2[] {new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1)};
mesh.triangles = new int[] {0,1,2, 1,2,3, 2,3,4, 3,4,5, };
mesh.RecalculateNormals();
gameObject.AddComponent<MeshCollider>();
GetComponent<MeshCollider>().sharedMesh = mesh;
GetComponent<MeshCollider>().convex=true;
}
}
I know I am not editing tangents of vertices, but from what I read i understood that's used mainly just for some lighting systems and shaders, Am I right ? Could someone tell me what I messed up or just push me to right direction ?
// Edit: I noticed that when I pause the game and take a look at collider mesh in inspector view, it looks right. However in real, in scene view, there are 2 extra triangles on collider connecting both side of object. Are there some specials rules for meshes ? So maybe unity is trying to fix something I did wrong when I initialised vertices and triangles ?
Thank you very much.
Answer by Eric5h5 · Oct 13, 2012 at 11:47 PM
You're making it a convex mesh, hence what you're getting is expected behavior.
Oh boy, I feel stupid I did not realized right away. Thank you.
Your answer
Follow this Question
Related Questions
Colliders on this spider 1 Answer
How to correct add collider to every child of gameobject? 1 Answer
Making two mesh colliders collide 2 Answers