- Home /
Trouble with OnCollisionEnter and Exit
Hello everyone, I've got a script that bridges a gap between the Unity physics system and scripted behavior (due to how a piece of the game needs to work) but I'm having a strange problem: after OnCollisionExit is called, it seems that OnCollisionEnter is being called and then OnCollisionExit is also called again.
More Details: Basically, the player is on a column that moves up and down (the player uses physics, but the columns do not). So, when the player jumps up and exits the column, the player inherits the calculated velocity of the column plus their own velocity from their jump.
Sometimes this works just as expected, but sometimes after the player exits the column collider, OnCollisionEnter is called for some reason, then OnCollisionExit is called again, meaning that the player inherits the velocity of the column twice (shooting them off into the air).
I have been unable to figure out why this is happening, so if anyone has an idea, I would love to know that the problem is.
void OnCollisionEnter (Collision other)
{
if (other.collider.tag == "Player")
{
Debug.Log ("something has enetered the column: " + other.collider.name);
other.transform.parent = transform;
}
}
void OnCollisionExit (Collision other)
{
if (other.collider.tag == "Player")
{
Debug.Log ("something has exited the column: " + other.collider.name);
other.transform.parent = null;
inheritedVelocity.x = xVel;
//Debug.Log ("inheritedVelocity X is " + inheritedVelocity.x);
inheritedVelocity.y = yVel + 1f; // + 5.5f;
Debug.Log ("inheritedVelocity Y is " + inheritedVelocity.y);
inheritedVelocity.z = zVel;
//Debug.Log ("inheritedVelocity Z is " + inheritedVelocity.z);
other.rigidbody.velocity += inheritedVelocity;
Debug.Log ("player's Y velocity is: " + other.rigidbody.velocity.y);
}
}
Answer by Jeff-Kesselman · May 05, 2014 at 11:43 PM
Its hard to tell just from your output, but my guess is that you have more then one collider, probably on different sub-objects of the model.
Maybe one for the whole player and another for the feet?
Definitely not 2 colliders on the column, I had that same idea that maybe the collider on the player body and player feet were causing it to fire twice, but I turned the feet collider off and it still happens.
I think I just might make some events to deal with the problem at this point.