- Home /
Keep boolean true whilst playing animation, then false
I have a little chunk of code within the Update function in order to detect the "Fire1" button/key being pressed. My code is as follows:
public bool Kicking;
void Update (){
anim.SetBool ("Kicking", Kicking);
if (Input.GetButtonDown ("Fire1")) {
Kicking = true;
}
}
I use 'Kicking' as a condition for the animation to occur
This causes the animation to be toggled upon using the "Fire1" input; however I am wanting to falsify the Boolean 'Kicking' directly after the animation has finished. ( I am going to be using this to check if the player is mid-kick when colliding with an object)
My initial thoughts on approaching this was to use the animation events. Upon adding the Player.Kicking Boolean as a property and making the Boolean true and false in the right places (beginning and end), I realised that the animation no longer plays, even if I don't modify anything at all. The Boolean is still modified to true but the animation does not initiate.
I'm not sure as to how effective it would be to wait for the animation to finish within the player script as I'll be using it for other things, any help will be greatly appreciated.
Answer by meat5000 · Oct 05, 2015 at 05:37 PM
Detect the currently playing clip
anim.GetCurrentAnimatorStateInfo(0).IsName("MyAnimationName")
//or something to that effect
if(!anim.GetCurrentAnimatorStateInfo(0).IsName("Kicking"))
{
anim.SetBool("Kicking", false);
}
Something like that. Its crude.
I would add an exit time parameter.
Your answer
Follow this Question
Related Questions
Set boolean to false when animation changes? 1 Answer
How to make an animation play after releasing key? 1 Answer
Animation not at correct position W/Video 0 Answers
Animation Not Playing - VideoExplanation 2 Answers
How do I know when an object is going under a Platform Effector 2D object? 0 Answers