- Home /
How to destroy player when collide with enemy?
I have a game object named Player which has a box collider and a rigidbody2D without kinematic. I also have a game object named Enemy, which has a circle collider with "is trigger on" and an animation applied to it. I want to know the code to make my player die when it collides with the enemy. It is a 2d game. I am trying to make a game similar to the "Worlds Hardest Game" This is the code im using
function OnCollisionEnter(myCol: Collision){
if(myCol.gameObject.name == "Enemy"){
Destroy(myCol.gameObject);
} }
Answer by JamesBrodski · May 10, 2016 at 02:50 PM
I figured it out, I had to put the word 2d infront of the function
Answer by rav3tey · May 10, 2016 at 07:00 AM
You can use :
function OnTriggerEnter (other : Collider )
{
if (other.CompareTag("Player")
{
//Game Over
}
}
Do note u need to use collider in the OnTriggerEnter params.
Answer by elz50 · Nov 18, 2020 at 10:51 PM
public Collider2D player; public Collider2D enemy;
private void SomeMethod() { if (player.IsTouching(enemy)) { destroy(player, delay in seconds); } }