#How to make enemy delete itself when coming into contact with player layer mask

1 messages · Page 1 of 1 (latest)

coarse pumice
#

How do I make my enemies delete their game object (themselves) from the scene. The code I have so far is:

using UnityEngine;

public class EnemyDiesOnImpact : MonoBehaviour
{
[SerializeField] private LayerMask whatDestroysEnemy;
private Rigidbody2D rb2d;

// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
    rb2d = GetComponent<Rigidbody2D>();
}

private void OnTriggerEnter2D(Collider2D collision)
{
    if ((whatDestroysEnemy.value & (1 << collision.gameObject.layer)) > 0)
    {
        Destroy(gameObject);
    }
}

}

brittle torrentBOT
#
<:error:1413114584763596884> Command not found

There's no command called ode.

minor flicker
#

!code

brittle torrentBOT
minor flicker
#

anyways, can't you just make the enemies (or at least the trigger in question) only detect the player?

#

(are they even triggers?)

coarse pumice
#

I am a complete beginner here, I literally just copied some code from my bullet script and thought it may work here.

the hope was that it detects either when it is in collision with the player gameObject, or the player layerMask. It would then delete itself if it was in collision with them

minor flicker
#

did you read what i said

coarse pumice
#

yeah, but I don't quite understand what you mean, or how to do so.

minor flicker
#

right, then say so instead of just ignoring it lol

minor flicker
#

first off, are either of the colliders involved in the collision set to trigger?

coarse pumice
#

No, is that nessecary?

minor flicker
#

you won't get trigger messages then, you'll get collision messages instead

#

as for the former message, consider configuring the layermask to make the specific layer check unnecessary, if possible

coarse pumice
#

okay, I'll try it out!