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.

Question OnTriggerStay sometimes doesn't trigger?

Discussion in 'Animation' started by Katnip, Dec 7, 2022.

  1. Katnip

    Katnip

    Joined:
    Dec 8, 2014
    Posts:
    3
    It triggers sometimes when you stay still inside the collider but other times you have to move in order to trigger it. Sometimes I have to press the e key five times to get it to trigger. The collider is quite big so that's not it, I thought maybe it was my script?

    Code (CSharp):
    1.  
    2. void OnTriggerStay(Collider other) {
    3.  
    4.         if (Input.GetKeyDown("e"))
    5.         {
    6.             if (other.CompareTag("Player"))
    7.             {
    8.                 if(ButtonAnimator.GetBool("Down") == false) {
    9.                     ButtonAnimator.SetBool("Down", true);
    10.                     // This is here to try to get the animation to play before the sound
    11.                     SawAnimator.SetTrigger("SawAnimationTrigger");
    12.                     audio.clip = idle;
    13.                     audio.loop = true;
    14.                     audio.PlayDelayed(13);
    15.                 } else {
    16.                     ButtonAnimator.SetBool("Down", false);
    17.                     SawAnimator.SetTrigger("SawAnimationTrigger");
    18.                     audio.Stop();
    19.                 }
    20.             }
    21.         }
    22.  
    23.     }
    24.  
     
    Last edited: Dec 7, 2022
  2. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    31
    This is Physics problem: OnTriggerStay may not be called on every (visual) frame, so it has a chance to be not called when GetKeyDown("e") is true.
    Physics event functions will be called only when Physics decide to evaluate this frame (= when FixedUpdate is called).