- Home /
Enemy does not destroy player in 2D
I am creating a top down shooter and when my enemy collides with a player I want it to be destroyed until I code what else I want it to do. With the code I have it doesn't destroy it. They both have collides and both have rigidbodys
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
Destroy(collision.gameObject);
}
}
Answer by tormentoarmagedoom · Sep 29, 2019 at 12:34 PM
Hello there!
First, This script should be attached to the enemy objects.
What kind of collider component have? must be Colliders2D (not colliders)
Then, to know if OnCollisionEnter2D is beeing called, make a debug.log to check in the console if its beeing called
if (collision.gameObject.tag == "Player")
{
Debug.Log ("Collision detected!");
Destroy(collision.gameObject);
}
And finally, colliders must not be marke as "trigger", if they are, you must use OnTriggerEnter2D.
Good luck!
It is a circle collider 2D and I am sure it is not marked as trigger, im trying out the script right now. I will report back soon
Yeah it's not even detecting the collision with the player :(
Give an explanation of what was the problem so other users can solve the problem :D
The solution to the problem was that they were kinematic but they needed to be dynamic. That’s all I had to do and since it was a top down 2D game I set the gravity to 0
Your answer
Follow this Question
Related Questions
Not destroying clone after interacting with other gameobject 1 Answer
Make the bullet in a 2d Top Down shooter get destroyed after passing mouse position 2 Answers
Collider wont collide. But I have done everything correctly. 1 Answer
Destroy by Tag isnt working,Can't Destroy By Specific Tags 2 Answers