- Home /
Animator parameter stuck as True
Hi! I'm using the animator controller for my "attack" animation. It works like a charm if i press the button that's supposed to trigger the animation in a gentle speed like once every second. But if i happen to spam press the button let's say 3 times a second the parameter im using gets stuck as True and the animation gets stuck at the last frame of my attack Animation and wont return to Idle.
if ((Input.GetKey (KeyCode.V)) && !anim.GetBool ("Attack"))
anim.SetBool ("Attack", true);
Here's the code i use to trigger the attack animation, i have an Animation event at the last frame of my attack animation to set the Bool back to false so he will return to idle again.
void StopAttack()
{
anim.SetBool ("Attack", false);
}
Please, show us how the transitions to and from the attack state are configured.
@DiegoSLTS, I removed that answer, my count was off this morning and you're right, made no difference.
Im sorry if im frustrating now but how would a picture with good information about the transitions look like?
try changing it to Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.V) and see if that helps any.
I now noticed that it happens if i hold down the key for 2-3 seconds it gets stuck. Do i need some kind of if statement like Get$$anonymous$$eyUp to make it true aswell perhaps?
Answer by hnmikechan · Jun 26, 2015 at 08:46 PM
I like putting my event handlers for functions similar to the StopAttack() at frame 1 or 2 of an animation. I don't think there is a reason to call it at the end. Once the transition is triggered there is no reason to keep the condition bool on.
Answer by The_Guy · Jun 26, 2015 at 05:35 PM
Okay, I think this will help. Set up your animator like this:
Add the "isAttacking" bool to your parameters in the top left of the animator panel. Make sure there are arrows to and from Idle and Attack. Then select the attack state and look in the inspector.
Do the same for the Idle state except isAttacking would be false.
Then have your code like this:
if ((Input.GetKeyDown (KeyCode.V)) && !anim.GetBool ("isAttacking"))
anim.SetBool ("isAttacking", true);
and use however you did to set isAttacking to false again.
The thing is, this is exactly how it is when it's causing the problem..
hmm, well idk what kind of game you are making but maybe implementing a cooldown timer would work. something like
bool canAttack = true;
float attackTimer = 0;
public float attackCooldown;
..........
if ((Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.V)) && !anim.GetBool ("isAttacking") && canAttack){
attackTimer = 0;
canAttack = false;
anim.SetBool ("isAttacking", true);
}
.........
Update(){
if(!canAttack){
attackTimer += Time.deltaTime;
if (attackTimer > attackCooldown) canAttack = true;
}
}
It's a topdown 2D game, it's just a very simple attack animation and i dont get how other people havn't had this problem..
Answer by kathy12345 · Sep 16, 2016 at 11:38 AM
Mine was stuck on true because I had forgotten to add a Condition in the inspector panel for the transition.
Answer by yourecrippled · Mar 17, 2017 at 02:25 PM
animator controller > click on state > uncheck "write defaults"
I did this to all states in my animation controller. It worked for me.
Your answer
Follow this Question
Related Questions
Play animation while button is pressed fails when using boolean parameters. 1 Answer
Blend Tree Transition Conditions. 0 Answers
Stop Animation State in Animator from Speeding Up after Playing it a Second Time? 0 Answers
What is the proper way to organize animator transitions to a large amount of abilities? 0 Answers
Double collision on "ground" tagged object results in player thinking they're in air? 1 Answer