Here's another way of implementing player death

Here is other version of managing player death.

First of all there is no need to change any collider to triger.
Thanks that is not an part of update it should work slighty better from the viewpoint of performance.

Here is whole code:

// On collision.
private void OnCollisionEnter2D(Collision2D collision)
{
 // If player is dead then exit from function.
 if(this.is_dead)
 {
   return;
 }
 // If player didn't colided with enemy then exit from function.
 if(collision.gameObject.layer!=LayerMask.NameToLayer("enemies"))
 {
   return;
 }
 // Change flag.
 this.is_dead=true;
 // Run animation.
 this.anim.SetTrigger("death");
 // Add random velocity.
 this.rbdy.velocity = new Vector2(Random.Range(15.0F,25.0F),Random.Range(15.0F,25.0F));
} // End of OnCollisionEnter2D
1 Like

Privacy & Terms