Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question OnTriggerEnter sometimes fails to be triggered when teleporting into Box Collider

Discussion in 'Physics' started by coska, Mar 21, 2023.

  1. coska


    Dec 8, 2022
    I'm putting this in the Physics forum, cause even though it has to do with music triggers, I think the problem is with Physics.

    Here's my issue:
    I'm using the script below to set various Music States in Wwise. When the player enters Music Region A, it triggers Wwise to change the state to Music A, etc. The system works flawlessly when the player is walking/running in and out from these box colliders. The problem is when the player teleports from one region to another. Sometimes Unity recognize the enter trigger (On Trigger Enter State) and sometimes it doesn't. It seems completely random. To add to my confusion, the EXIT trigger (On Trigger Exit State) trigger always works 100% of the time.

    Attached is a screenshot of how I've set up the trigger region.

    I've tried adding a Rigidbody to this object (with various settings on the Is Kinematic / Collision Detection options) with no effect.

    Any suggestions would be very appreciated! Thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class SetMusicState : MonoBehaviour {
    6.     public AK.Wwise.State OnTriggerEnterState;
    7.     public AK.Wwise.State OnTriggerExitState;
    8.     private void OnTriggerEnter(Collider other)
    9.     {
    10.         if (other.CompareTag("Player")) {
    11.             OnTriggerEnterState.SetValue();
    12.         }
    13.     }
    14.     private void OnTriggerExit(Collider other)
    15.     {
    16.         if (other.CompareTag("Player"))
    17.         {
    18.             OnTriggerExitState.SetValue();
    19.         }
    20.     }
    21. }
  2. coska


    Dec 8, 2022
    More info: I did a Capture Log with Wwise while running the game. Wwise DO get every single trigger, but since they arrive at the EXACT same time (not even a 1/100 variation) they sometimes (randomly, it seems) arrive in the wrong order, triggering Enter BEFORE the Exit trigger, which puts the wrong Music State in Wwise.
    What can be the cause of this? Is there a way to put a slight delay (let's say one frame) on the Enter trigger, so it always end up AFTER the Exit trigger. It doesn't feel like the "right" solution, but it would solve this issue. Would be thankful for any ideas.