OnTriggerEnter acting funny.

I have a player sphere with RigidBody, SphereCollider and two empty child gameObject both have RigidBody, SphereCollider as the trigger, and a script of their own to handle the OnTriggerEnter. One is bigger to check if the enemy was in sight range, and another one is smaller to check if the enemy is in attack range.

When I test it out, it worked for “couple times” and then it stop working!!!

Could somebody please tell me what did I do wrong, please!?

Much appreciated!!!

In sight trigger:

public class Trigger : MonoBehaviour {

    private void OnTriggerEnter(Collider other)
    {
        if(other.tag=="Enemy") Debug.Log("Enemy in sight!");
    }
}

Attack trigger:

public class Attack : MonoBehaviour {

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Enemy") Debug.Log("Attack!");
    }
}

Hi @unityChu

I’ve never tried this kind of setup, where you have overlapping colliders… it sounds a bit questionable.

This is what I would try instead: Have one collider, that is of radius for enemy in sight, and use distance for decision making.

In OnTriggerStay, instead of OnTriggerEnter, check the Vector3 distance to your player.

If distance is bigger than attack distance, enemy is just in sight. Else attack.

Alternatively, you could try this kind of hierarchy - you only need one RigidBody, and child objects can have the collider triggers and script - one would have Attack component, other would have Trigger component. This seems to be working OK, only either gets triggered, when passing through it. NOTE: I tried this for some 20 times, seems to be working OK.