- Home /
Play animation while button is pressed fails when using boolean parameters.
All right, I got my GameObject with its animator and several states and parameters.
My "need" is making an animation play while a button is pressed (You press for X seconds the button, the animation plays for X seconds).
So, I got my "Idle state"(IS), my "Playing animation State"(PAS). The transition from IS to PAS uses a boolean parameter "buttonPressed" and the condition value is "true".
There's some code around to handle this, and when the user presses a button, the code executes Animator.SetBool("Transition", true);
So far, so good: The state jumps from IS to PAS, and the animation starts to play. The problem is that, somehow, the value of the boolean resets, executing the transition from PAS to IS, resetting the state machine and re-executing the transition from IS to PAS.
So, I'm scratching my head on what could be wrong here: Is my approach to keeping the animation playing wrong? I'm ignoring any other "right" way to do this? Should I re-check any value on the animator?
Answer by Elendow · Jul 03, 2017 at 09:25 PM
Hi,
Generally, the problem with "input loss" is due to check this input on the FixedUpdate. This problems appear because the input is updated at the beginning of every frame, but the FixedUpdate can be called outside that frame or multiple times in it.
The best you can do is to check the ups and downs of the input on the Update in order to have the freshly updated input values.
If doing this check in the Update is not possible, another solution is to check the "GetButton/GetKey" instead of "GetButtonDown/GetKeyDown" and "GetButtonUp/GetKeyUp". Generally this value works fine on the FixedUpdate, but not always.
Well, it seems I can't use the GetButton/ButtonDown, because those need the keys to be mapped with the default "Input" system, which seems everyone suggested to not use (And given my game is for 4 players on the same computer...)
Anyway, as I got my own input controller, I switched the functions (as I got one for updates and another for fixedInputUpdates, something to do with physics and stuff), the animation played as it was supossed to do.
Thanks!
You can make your own keymappings like this.
public $$anonymous$$eyCode action$$anonymous$$ey;
void Update()
{
if (Input.GetButtonDown(action$$anonymous$$ey))
{
Debug.Log("Action $$anonymous$$ey Press");
}
}
Hey, I was editing the comment right when you answered, lol.
Input.getbuttonDown is asking me for a String parameter, not an Action$$anonymous$$ey, and when going "I'm gonna be lucky" (Using the ToString function), Unity just tells me to bind the keys on the Edit->Whatever menu, which is a nope.avi, as all our input system was written based on the suggestions of "not using Unity's default input system."
I'll see if I can do something with this. Thanks anyway!
Your answer
Follow this Question
Related Questions
Animator parameter stuck as True 4 Answers
Jump animation anticipation 2 Answers
Dynamically add AnimationState to Controller 0 Answers
Animations will play for a second perfectly then stop HELP 1 Answer
Pause Menu issue with mouse 1 Answer