So I have an enemy that has a circle collider to detect the player so they can start attacking. Here is the code I’m using to detect the player. Once the player is detected the enemy activates, but I can’t find a way to detect that the player has left so I can deactivate the enemy. Is there a way to do that with my existing code or is there a better way to do this entirely.
If the enemy has a CircleCollider component already, why not use the OnTriggerEnter/OnTriggerExit callbacks?
You also need some way to determine if any of the detected colliders actually belong to the player object. The way you have it setup right now, “active” will be set to true when any collider is detected.
A tag-comparison is one possible way to do that:
void OnTriggerEnter(Collider collider) {
if(collider.gameObject.CompareTag("Player")) {
active = true;
}
}
void OnTriggerExit(Collider collider) {
if(collider.gameObject.CompareTag("Player")) {
active = false;
}
}
It doesn’t have a circle collider component. The script is drawing a circle around it. I’m still kind of new to unity so I don’t know what the difference is but when I tried using on trigger enter and a circle collider it was messing with my character controller.