Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Trigger issues (multiple events, enter/exit)

Discussion in 'Scripting' started by cbfunky, Sep 28, 2020.

  1. cbfunky

    cbfunky

    Joined:
    Sep 11, 2020
    Posts:
    24
    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!

    trigger events.PNG

    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TriggerScript : MonoBehaviour
    6. {
    7.     public Collider player;
    8.     public Material activeMaterial, inactiveMaterial;
    9.     public float TriggerVolumeCooldown = 1f;
    10.  
    11.     //private int i;
    12.     private Renderer m_Renderer;
    13.     private bool m_IsOnCooldown;
    14.     private float m_CooldownStartTimestamp;
    15.  
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         m_Renderer = GetComponent<Renderer>();
    21.         m_Renderer.material = inactiveMaterial;
    22.         m_IsOnCooldown = false;
    23.     }
    24.  
    25.     void OnTriggerEnter(Collider collider)
    26.     {
    27.         //i++;
    28.         if (collider == player && !m_IsOnCooldown)
    29.         {
    30.             print(" Player entered " + collider.name + " CD: " + m_IsOnCooldown + " xyz: " + collider.GetComponentInParent<Transform>().position);
    31.             m_Renderer.material = activeMaterial;
    32.             m_IsOnCooldown = true;
    33.             m_CooldownStartTimestamp = Time.time;
    34.         }
    35.         else
    36.         {
    37.             print("cooldown");
    38.         }
    39.     }
    40.  
    41.     void OnTriggerExit(Collider collider)
    42.     {
    43.         //i++;
    44.         if (collider == player)
    45.         {
    46.             print(" Player left " + collider.name + " xyz: " + collider.GetComponentInParent<Transform>().position);
    47.             m_Renderer.material = inactiveMaterial;
    48.         }
    49.     }
    50.  
    51.  
    52.  
    53.     // Update is called once per frame
    54.     void Update()
    55.     {
    56.         if (m_IsOnCooldown)
    57.         {
    58.             if (Time.time - m_CooldownStartTimestamp > TriggerVolumeCooldown)
    59.             {
    60.                 m_IsOnCooldown = false;
    61.             }
    62.         }
    63.     }
    64. }
    65.  
    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?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    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.
     
  3. cbfunky

    cbfunky

    Joined:
    Sep 11, 2020
    Posts:
    24
    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:

    player.PNG

    And this is the platform:
    platform.PNG
     
    Last edited: Sep 28, 2020
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @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:
     
    cbfunky likes this.
  5. cbfunky

    cbfunky

    Joined:
    Sep 11, 2020
    Posts:
    24
    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!