- Home /
updating MeshCollider, but OnCollisionExit() not working
I need to dynamically update my collier. OnCollisionEnter() and OnCollisionStay() were called when the collider enters a Riggybody, but why OnCollisionExit() won't be called when the collider exits Riggybody?
void Update()
{
//Update MeshCollider
MeshFilter mf = gameObject.GetComponent<MeshFilter>();
gameObject.GetComponent<MeshCollider>().sharedMesh = mf.sharedMesh;
}
void OnCollisionEnter()
{
Debug.Log("OnCollisionEnter called.");
}
void OnCollisionStay()
{
Debug.Log("OnCollisionStay occuring.");
}
void OnCollisionExit()
{
Debug.Log("OnCollisionExit called.");
}
You are missing the collision parameters in your example.
He's not missing the parameter, he has omitted the parameter which is possible for those callbacks:
If you don't use collisionInfo in the function, leave out the collisionInfo parameter as this avoids unneccessary calculations.
Answer by Bunny83 · Apr 05, 2019 at 10:21 AM
What you do here is generally a bad idea. First of all Unity's phyics system is a rigidbody simulation. A rigidbody is a rigid body. So one object doesn't change it's shape. Any object that is moving / rotating has to have a rigidbody attached. Colliders without a rigidbody component must never be moved / rotated / changed.
I know in the past it wasn't possible to update a MeshCollider like that. Maybe this got fixed. Anyways recalculating / recreating a meshcollider every frame is very bad for performance and certainly won't produce any realistic rigidbody movement.
For moving objects that should not be controlled by the physics engine but are moved by script need a kinematic rigidbody. Note that for collisions to happen one of the two colliders need to belong to a non-kinematic rigidbody. So two kinematic rigidbodies can't collide with each other.
Answer by Understainding · Sep 29, 2020 at 03:29 PM
OnCollisionEnter is executed when an object enters the current mesh. OnCollisionStay is executed as the object remains in. As soon the mesh collider gets modified, the collider internal data changes.
Therefore the it does not remember what is inside so it will not call OnCollisionExit.
Console Debug Log is persistent and does not reflect current internal state of the collider.,This is probably because you rebuild the mesh any memory of it gets erased. That is, as an obstacle enters the mesh, It triggers OnCollisionEnter, as long as the collider is not modified then exexutes OnCollisionStay. But as soon, as it is modified it stoops. For it is, a new [mesh] collider all together.