Trigger issues (multiple events, enter/exit)

Hey guys, not sure if this is a scripting issue or if I’ve implemented something wrong with the colliders, but here it goes:

All I want is a simple platform trigger that registers when the player steps on it and when they get leave.
I have BoxColliders with IsTrigger on both the platform and the player and in general everything works okay.

However there are some strange edge cases where the OnTriggerExit happens immediately after the OnTriggerEnter. Once I’ve even managed to stand in a place where it constantly went from Enter to Exit to Enter etc. without the player moving at all!

So I put position outputs in the debug text and the positions of where the Enter/Exit happens is in really strange places, nowhere near the trigger volume!

Initially I put a cooldown on the trigger volume, so it could only trigger every second but that doesn’t solve the issue of the Exit triggering for some reason when it shouldn’t.

Here’s my code for the platform:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TriggerScript : MonoBehaviour
{
    public Collider player;
    public Material activeMaterial, inactiveMaterial;
    public float TriggerVolumeCooldown = 1f;

    //private int i;
    private Renderer m_Renderer;
    private bool m_IsOnCooldown;
    private float m_CooldownStartTimestamp;


    // Start is called before the first frame update
    void Start()
    {
        m_Renderer = GetComponent<Renderer>();
        m_Renderer.material = inactiveMaterial;
        m_IsOnCooldown = false;
    }

    void OnTriggerEnter(Collider collider)
    {
        //i++;
        if (collider == player && !m_IsOnCooldown)
        {
            print(" Player entered " + collider.name + " CD: " + m_IsOnCooldown + " xyz: " + collider.GetComponentInParent<Transform>().position);
            m_Renderer.material = activeMaterial;
            m_IsOnCooldown = true;
            m_CooldownStartTimestamp = Time.time;
        }
        else
        {
            print("cooldown");
        }
    }

    void OnTriggerExit(Collider collider)
    {
        //i++;
        if (collider == player)
        {
            print(" Player left " + collider.name + " xyz: " + collider.GetComponentInParent<Transform>().position);
            m_Renderer.material = inactiveMaterial;
        }
    }



    // Update is called once per frame
    void Update()
    {
        if (m_IsOnCooldown)
        {
            if (Time.time - m_CooldownStartTimestamp > TriggerVolumeCooldown)
            {
                m_IsOnCooldown = false;
            }
        }
    }
}

So multiple questions, I guess:

  1. Why is the enter/exit “flicker” happening - is it the colliders?
  2. Why is the enter/exit position in such a strange place?

Hi @cbfunky

It is pretty hard to say anything about your colliders - you didn’t share any details or images.

“I have BoxColliders with IsTrigger on both the platform and the player and in general everything works okay.”

Why would you have a trigger collider in player? If your player is already moving with physics system, then why not use that collider, and let the floor trigger have the trigger collider.

Ah, this is actually a good point. I’m really new to Unity so I just assumed you needed to have IsTrigger checked in order to trigger things - but from your question it seems to me you only need it to be triggered. Could this be the issue? I’ll try running it without the Istrigger on the player collider. (EDIT: I unchecked IsTrigger on the player collider, but the issue persists).

I’m not entirely sure what you mean by “is already moving with physics system, then why not use that collider” - are you talking about the rigidbody?

This is my player setup for this case:

6357564--707220--player.PNG

And this is the platform:
6357564--707226--platform.PNG

@cbfunky
“I’m not entirely sure what you mean by “is already moving with physics system, then why not use that collider” - are you talking about the rigidbody?”

Yes, I meant that I was guessing based on your image… if you are using a capsule as character controller, you most likely already have either a capsule collider + rb or a CharacterController (+rb) which is a collider.

I don’t see anything obvious in those inspector images. Have you checked that you actually see the collider in your platform, and it is overlapping player’s collider?

And maybe compare your setup to some working example like this one:

1 Like

Ah, I didn’t realize the CharacterController is also a collider. I currently have a rb, capsule and character controller. I’ll play around with those and see if that helps!

I’m pretty new to unity, so I haven’t figured out yet how to show debug stuff in the game view so I can’t 100% say about the overlap, but from the static setup it looks ok (and I mean it works 99% of the time).

I’ve changed my implementation approach now, which majorly reduces the enter/exit flicker occurrance: instead of acting on the actual triggers I’m setting a bool to “HasPlayerInside” and then when the conditions allow, trigger the occurance (i.e. you can enter on cooldown, remain in the volume and then trigger the event when the cooldown is over without having to step off and on again).

Thanks for the tutorial link!