Search Unity

Question How to finish animation after exiting trigger

Discussion in 'Animation' started by elcuci, Jul 5, 2021.

  1. elcuci

    elcuci

    Joined:
    Jun 13, 2021
    Posts:
    17
    Hey!

    So I have this gate kind of thing in my game. When you get inside the trigger, it opens, when you get out it closes. Im using an animation event called pauseAnimationEvent that pauses the animation when it reaches the mid point (when the gate is opened). If you exit the collider after that its re-enabled. The thing is, if you exit the collider before it reaches the pauseAnimationEvent the animation just stays there stays there.

    I believe its because the player has already exited the trigger, so the program doesnt know that it is supposed to release the pause event and finish the animation. If you enter and exit the trigger again it finishes it because it detected that the player exited the trigger.

    Heres the script I have.

    Code (CSharp):
    1. public class DoorOfTheOpen : MonoBehaviour
    2. {
    3.     Animator anim;
    4.     void Start()
    5.     {
    6.         anim = GetComponent<Animator>();
    7.     }
    8.  
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         if (other.CompareTag("Player"))
    12.         {
    13.             anim.SetTrigger("OpenDoor");
    14.         }
    15.     }
    16.  
    17.     void OnTriggerExit(Collider other)
    18.     {
    19.         anim.enabled = true;
    20.     }
    21.  
    22.     void pauseAnimationEvent()
    23.     {
    24.         anim.enabled = false;
    25.     }
    26. }
    27.  
    I was thinking maybe it could be solved by having a function that checks if the player is still in the trigger, if its not then it skips the pauseAnimationEvent void. Or maybe theres something built in to Unity that lets you do this. Im not sure.

    I hope I expressed myself at least more or less well. If you have any doubts feel free to ask.

    Also, my keyboard broke and I cant use apostrophes, sorry for that!

    Thanks in advance!!