OnTriggerExit not being called with CharacterController?

So I’m trying to make a cone of vision type trigger that sets an alert status to true when the player enters the trigger, and to false when they leave. My player uses a CharacterController. The problem is that OnTriggerEnter works fine, but OnTriggerExit does not (i.e. it is not called when the player leaves the trigger). I’ve done some hunting on the forums and looked at other questions but haven’t really found anything that seems to help. Even though it’s simple I’ll post my code just in case I made some silly mistake (That happened last time I posted a question :P):

public bool alert = false;
    public Collider target;

    void OnTriggerEnter(Collider c)
    {
        if (c.gameObject.tag == "Player")
        {
            target = c;
            alert = true;
        }     
    }

    void OnTriggerExit(Collider c)
    {
        if (c.gameObject.tag == "Player")
        {
            alert = false;
            target = null;
        }
    }

Thanks!

I don't think i've ever tried to use OnTrigger stuff with CharacterControllers, so you may first want to test puting a box collider(s) over/near/around your character controller.

1 Answer

1

There are many ways to do this without using colliders.

  1. Renderer.isVisible
  2. Raycasting
  3. Trigonometry

I would suggest option 3; it’d probably be the fastest and most accurate. :slight_smile:

It's a solution yes, but not helpful for others trying to solve the issue with OnTriggerExit