- Home /
When Pressing a key to animate it is sometimes responsive sometimes it is not how can I fix this? PLEASE HELP!
What Is happening is I press the key and sometimes it will trigger the animation some times it will not, Sometimes it will trigger the animation and quickly go back to the idle, sometimes it will play the jumping animation in a loop over and over again and sometimes it will work completely fine. IT'S DRIVING ME BONKERS! lol Please Help Me.
if (isGrounded)
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
_animator.SetTrigger("IsJumping");
isGrounded = false;
}
else
{
_animator.SetBool("IsFalling", false);
_animator.SetBool("Landing", true);
}
}
else
{
_animator.SetBool("IsFalling", true);
}
private void Crouch()
{
IsCrouching = !IsCrouching;
if (Input.GetKeyDown(KeyCode.C))
{
if (IsCrouching)
{
_animator.SetBool("IsCrouching", false);
}
else
{
_animator.SetBool("IsCrouching", true);
}
}
}
private void Punching()
{
IsPunching = !IsPunching;
if (Input.GetKeyDown(KeyCode.F))
{
if (IsPunching)
{
_animator.SetTrigger("IsPunching");
}
else
{
_animator.SetTrigger("DontPunch");
}
}
}
Can you edit your post and add a screenshot of your animator controller network, and a screenshot or snippet of the code that is causing the issues?
or at least put the piece of code that handle the key press
It's hard to say exactly what is happening in the animation network because I can't really tell what bools trigger what animations and block others. I would assume that "has shit time" on some animation is set to true. Try setting all animations' "has exit time" to false to test if that works.
first question: on each function, dont need to invert the bool, after check the key down, inside the if?
by example on punching
private void Punching()
{
//I delete from there
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F))
{
//and put it there
IsPunching = !IsPunching;
if (IsPunching)
{
_animator.SetTrigger("IsPunching");
}
else
{
_animator.SetTrigger("DontPunch");
}
}
}
This is true. And, it must be reset after. Otherwise, it will only trigger every other animation.
humm, I saw it as a toggle, like, "press F to start punching and continue punching after press F again" if he want to "each time press F punch once " he dont need to set a trigger to stop punch, just set the "return to idle" transition with no conditions and set a bool on transition "idle to punch"
private void Punching()
{
//I delete from there
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F)){
_animator.SetBool("IsPunching", true);
}else{
_animator.SetBool("IsPunching", false); ;
}
}
if he want to "hold F to Punch and keep punching" he can use a bool too, but need to check the same variable but the value to false to return, and use Get$$anonymous$$ey
private void Punching()
{
//I delete from there
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.F)){
_animator.SetBool("IsPunching", true);
}else{
_animator.SetBool("IsPunching", false); ;
}
}
THIS WOR$$anonymous$$ED! Thank you. YOU DA $$anonymous$$AN!
n.n glad to help, also follow the @Adam_g friendly advise and keep simple your animator
Answer by ray2yar · Feb 08, 2019 at 02:36 AM
Did you make sure "has exit time" is unchecked?
If I keep hitting the key over and over and over again the animation will finally play, it's almost like my keyboard isn't registering the keys but I know it's not my $$anonymous$$eyboard because it's brand new lol it's gotta b something with my code or the anim controller
Answer by Chimer0s · Feb 08, 2019 at 03:17 AM
Is this happening with all of the animations or only specific ones?
It is happening with the crouch and Punching animations
Answer by Adam_g · Feb 08, 2019 at 03:21 AM
I ran into a similar problem recently. I was checking for a keypress in FixedUpdate(), and the keypress was not being registered. Move the Input checks to Update and it should work.
FixedUpdate gets called by the game engine every physics step, which is independent of a time step and does not complete at the same rate. Since Input.GetKeyDown is only true for one frame, it is unlikely that that frame would land within a physics step and trigger the animation.
I do have them in Update so that's not the issue, Im so confused With this.