Search Unity

Question How to Deactivate a Trigger when PickUp animation is done?

Discussion in 'Animation' started by potter3366, Feb 21, 2023.

  1. potter3366

    potter3366

    Joined:
    Oct 15, 2009
    Posts:
    65
    I'm trying to add a new animation to StarterAssetsThirdPerson Controller (Playground scene) from Starter Assets - Third Person Character Controller. After pressing the P key, the animation is activated, played once and does not return to the "Idle,Walk,Run" state. I'm using Trigger Parameter (PickUp) as Condition and No "Has Exit Time". In the Unity Documentation it is written: "Triggers have a true option which automatically returns back to false."
    Am I missing something?

    p.jpg
     
    Last edited: Feb 21, 2023
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    So, if I'm understanding this correctly, you're expecting the trigger to return to false after the animation is done playing. This is not how triggers work.

    Setting a trigger to true will cause any transitions with that condition to fire, but the trigger will immediately return back to being false. However, in my experience, triggers are very finicky and don't always work how you might expect. For example, if you have a bunch of transitions firing off really quickly, the trigger may not return to false. This is why I don't like using them; however, this is not your issue.

    Your "Picking Up" state doesn't have an exit transition, so you're stuck in that state. If you want to exit that state after its animation is done playing, create a transition back to idle with an exit time of 1.

    If you're unaware of how exit time works, it's the normalized time (0-1) when an animation will transition. The 0-1 corresponds to the length of the animation. So if you set it to 0.5, an animation with a length of 6 seconds will exit half-way through, at 3 seconds. 1 is always the end of the animation, so in this case, you'd need to set it to 1.
     
    Last edited: Feb 22, 2023
  3. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    I got a bit curious about how Unity describes triggers, and I now understand why you thought your setup would work.

    "A typical example might be to have a Jump option. If this option is entered during run-time the character will jump. At the end of the Jump the previous motion (perhaps a walk or run state) will be returned to."

    I pulled this quote from here: https://docs.unity3d.com/ScriptReference/Animator.SetTrigger.html

    This is Unity's official documentation, and unless I'm missing something, it's just flat out wrong. Triggers only work one way, they never transition back automatically. You need to set up a transition back yourself. There is no way to replicate the behavior they're describing with only triggers; you need a transition back with exit time.
     
  4. potter3366

    potter3366

    Joined:
    Oct 15, 2009
    Posts:
    65
    With back transition, animator exit the state ("Picking Up"), but immediately afterwards returns to Idle in an infinite loop. Trigger remained active even with an exit time of 1. I also tried with bool condition, but without success..
     
  5. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Okay... what's the issue here then? An infinite loop? If you're using a bool, you need to set it to false when you enter the "Picking Up" state. If the trigger is getting stuck "true," then you need to manually set that to false as well.

    Here's a video example showing everything working: https://drive.google.com/file/d/1bE6ZMM_sisRekE4eIU7JtSu52_1dTqJ3/view?usp=share_link

    If you want to automatically set the bool to false when you enter the "Picking Up" state, you can write something like this:
    Code (CSharp):
    1.         if (anim.GetCurrentAnimatorStateInfo(0).IsName("Picking Up"))
    2.         {
    3.             anim.SetBool("Bool", false);
    4.         }
     
  6. potter3366

    potter3366

    Joined:
    Oct 15, 2009
    Posts:
    65
  7. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    What did you just link? You've given no explanation as to what's going on, and you linked a ZIP file. I'm not going to download that out of pure curiosity.

    You need to put a bit more effort into your responses if you're still struggling and want help.
     
  8. potter3366

    potter3366

    Joined:
    Oct 15, 2009
    Posts:
    65
    It's actually a Starter Assets - Third Person Character Controller scene with a pickup animation added. Tried all variants but I still have a ping-pong animation that I can't control with the buttons. It looks like the "P" key is pressed all the time. Maybe it's a problem with the new input system which is difficult to setup after all..
     
  9. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Tried downloading it to test, but windows is telling me it's invalid. Since there's no preview on onedrive, I'm guessing it became corrupted when you uploaded it.

    Are you sure that's the issue? You can test this really easily by adding a Debug.Log() line to your original code to see how many times it gets called in the console:
    Code (CSharp):
    1.         if (_input.pickup)
    2.         {
    3.             _animator.SetTrigger("PickUp");
    4.             Debug.Log("Set PickUp Trigger.");
    5.         }
     
  10. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    I don't have much experience with the new input system, but it looks like there's no intuitive way to mimic the functionality of
    Input.GetKeyDown()
    . This is an issue, because using
    Input.GetKey()
    doesn't work well with triggers; the 5-10 frames that you hold the button down causes the trigger to stay true.

    If you really want to use triggers, take a look at this thread on how to detect a keypress for a single frame: https://forum.unity.com/threads/how...eydown-keycode-a-in-new-input-system.1283438/
     
  11. potter3366

    potter3366

    Joined:
    Oct 15, 2009
    Posts:
    65
  12. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    You are correct, the P key is being held down every frame after you press it once. I tried messing around a bit, but I couldn't figure out why this was happening; all the actions I created had the same issue.

    If you want a quick and easy solution, just use this code instead:
    Code (CSharp):
    1.             if (Input.GetKeyDown(KeyCode.P))
    2.             {
    3.                 _animator.SetBool("pickupbool", true);
    4.             }
    If you want to figure out how to make this work using the new input system, I suggest you ask here: https://forum.unity.com/forums/input-system.103/

    It turns out, this isn't an animation issue.
     
  13. potter3366

    potter3366

    Joined:
    Oct 15, 2009
    Posts:
    65
    Thank you for your efforts.