How can I make an enemy hurt the player?

I am working on a simple prototype where I want to use a trigger box collider to cause damage to the player. The player uses a character controller. This trigger is attached to an enemy that walks around. My intent is to damage the player when it enters this trigger. It just turns out that OnTriggerStay won't fire if the player is standing still (if the enemy walks onto the player). I guess this might seem a bit backward but I'd really want to have a "damage trigger" attached to the enemy just because it feels natural. This won't work because it seems OnTriggerStay won't fire unless the player moved into the collider, not the other way around.

  • What do you suggest I'd do instead?

Also, the docs say:

This message is sent to the trigger and the collider that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigidbody attached.

I don't have any rigidbody though, but it surely responds to my character controller. Is it a fault in the docs or am I using OnTriggerStay wrong?

// My damage trigger code, that doesn't work if the player is standing still.
public class DamageTrigger : MonoBehaviour 
{    
    void OnTriggerStay(Collider collider)
    {
        collider.SendMessage("OnDamage", SendMessageOptions.DontRequireReceiver);
    }
}


Below are my status updates:

As suggested, I have tried using OnTriggerEnter and OnTriggerExit but it doesn't help.

// My damage trigger code, that doesn't work if the player is standing still.
public class DamageTrigger : MonoBehaviour
{
    List<Collider> colliders = new List<Collider>();

    void Update()
    {
        foreach (var collider in colliders)
        {
            collider.SendMessage("OnDamage", 
                SendMessageOptions.DontRequireReceiver);
        }
    }

    void OnTriggerEnter(Collider collider)
    {
        colliders.Add(collider);
    }

    void OnTriggerExit(Collider collider)
    {
        colliders.Remove(collider);
    }
}

Have you tried with another approach? don't use OnTriggerStay, but OnTriggerEnter and OnTriggerExit.

When entering the trigger, use SendMessage to tell the player object to change into a "hurting continously" status. When exiting that trigger, use SendMessage to end that "hurting" status.

That way the main code is on player, but the triggers remain useful enough ;)

I bypassed this by calculating the distance between the trigger and player object.

“if the distance is less than collisionbox = triggered”

dunno how efficient, but it works.

Thought I’d clean up my old questions.

A trigger moving into another trigger must also have a rigidbody.

Any physics object that is moving must have a rigidbody for its callbacks to trigger.

Also, moving objects with a collider (trigger or not) that does not have a rigidbody will incur a performance penalty since Physx will have to rebake the entire scenes static physics data. Colliders without rigidbodies fall into that category.


  • What I should have done back then was to ensure both moving triggers had a rigidbody on them.