- Home /
Assigning new sharedMesh to mesh collider every frame doesn't work?
So i am making a tron like game and i am trying to collide my bike trail against other colliders. I used TronTrail script from unity procedural examples. I simply added a Mesh Collider and at the end of every update assign current trail mesh to sharedMesh of mesh collider. When i try to collide my bike against it, it doesnt work. My layer setup is correct, my bike doesnt use Mesh Colliders and isnt kinematic. Any ideas?
ScreenShot from paused game:
What type of collider does the bike have? As per the manual, Unity is a little bit picky about which mesh colliders can or can't actually collide with each other (for performance reasons).
Also, what's the thickness of that mesh collider? I don't have experience using 0-thickness mesh colliders, but some of the others don't handle that well.
Bike has 2 wheel colliders and a box collider. No mesh colliders. Yes trail has 0 thickness. But i tried the same thing using extrusion demo in the same unity package. $$anonymous$$ade a base mesh for extrusion and same as here applied new mesh(non zero thickness) to shared$$anonymous$$esh of the collider every frame. Same result.
Answer by simejanko · Aug 09, 2014 at 10:04 AM
I figured it out. I only called meshCollider.sharedMesh = mesh; every frame. I added meshCollider.sharedMesh = null; a line before that and it works.
This isn't working for me (Unity 5.3.2.f1 Personal). I've tried adding the $$anonymous$$eshCollider at runtime, setting it to null first and a combination of the two. It is very strange as the mesh collider will snap to the parent object if I move it in the Scene view. If I do a transform.Translate in script it doesn't trigger the 'snap'. Note the origin of the gameobject is in a weird place before the update:
Code:
$$anonymous$$eshCollider mc = gameObject.AddComponent<$$anonymous$$eshCollider>();
mc.shared$$anonymous$$esh = null;
mc.shared$$anonymous$$esh = poly.mesh;
EDIT: I fixed this by creating the collider after setting the $$anonymous$$eshFilter to 'poly' (I was doing it the other way round before...)