- Home /
How can I setup my bool/trigger in animator?
Hi, I'm new to Unity (and C#) and trying to link my bool and my animation in animator to play correctly.
I have my bool:
bool shoot = Input.GetButtonDown("Fire2");
And I have my animation set up on the player and it is playing (just looping infinitely despite the looping box being unchecked for the animation.
How can I get my bool to activate the parameter "shoot" in animator? I've tried to set a bool and a trigger, but can't seem to get either to play the animation once on button press ("Fire2") and then set to false. Any help would be greatly appreciated!
Trigger is what you want for a "one-shot" state change trigger. What exit conditions do you have in the shooting animation state in the animator? Assu$$anonymous$$g these are met, it should transition straight away once the animation has played.
Hi tanoshimi and thanks for the comment! It looks like I had a bunch of views before you chimed in, but no advice :) It turns out the trigger is what I wanted and I have the exit conditions set as the name of my trigger in the animator parameters. It's mostly working now and I think if I just adjust my animations a bit, it'll be fine. What I ended up doing was calling for the animator using GetComponent (again) right after my bool, then I could use SetTrigger without any problems for the animation!
Answer by Danao · Sep 16, 2014 at 03:37 AM
Here is the snippet of code that worked for me in case someone comes across this with the same problem:
bool shoot = Input.GetButtonDown ("Fire2"); //Attack button and animation
if (shoot) {
Animator animator = GetComponent<Animator>() as Animator;
animator.SetTrigger("Attack");
Your answer
![](https://koobas.hobune.stream/wayback/20220613163024im_/https://answers.unity.com/themes/thub/images/avi.jpg)