Odd issue with collision between player and enemy.

I use the following script:

void OnCollisionEnter(Collision collision)
{

if (collision.gameObject.tag == "Enemy")
{
    Destroy(this.gameObject);
    Application.LoadLevel(Application.loadedLevel);
}

}

The problem is that the collision only causes the player’s death if he moves into the enemy. If it’s the enemy who moves into the player nothing happens - the enemy just stands next to the player.

Any idea why this is happening?

@Matt5 Application.LoadLevel(Application.loadedLevel); will correctly reload the last level played. BTW, am I the only one bothered by the 1-Destroy(this) then 2-LoadLevel()?

Destroy wait the end of the frame to do its job. So nothing is wrong here.

1 Answer

1

It depends how the enemy is moved. If it’s not by physic (gravity, character controller, force etc) it won’t raise a collision event. One of the two collider involved must have a non-kinematic rigidbody.