- Home /
Animator not setting bool on GetButtonUp?
I'm trying to make an animation play when I press a UI button, and another one when I release it. It works for the GetButtonDown, but not GetButtonUp. Am I doing something wrong here? Thanks for help.
Here's the bit of code on that.
void FixedUpdate()
{
if(Input.GetButtonDown("AttackButton"))
{
anim.SetBool(P_ATTACKING, true);
}
else if(!Input.GetButtonUp("AttackButton"))
{
anim.SetBool(P_ATTACKING, false);
}
}
Answer by Aviryx · Aug 11, 2020 at 11:56 PM
So I originally thought you were trying to using a custom keybind but now see you actually have a UI button that is being pressed.
public Button attackButton;
void Start()
{
attackButton.OnClick.AddListener(StartAttack);
}
private void StartAttack()
{
anim.SetBool(P_ATTACKING, true);
}
For the next part I think you need OnMouseUpAsButton
https://docs.unity3d.com/2018.3/Documentation/ScriptReference/MonoBehaviour.OnMouseUpAsButton.html
"OnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed."
public Button attackButton;
void Start()
{
attackButton.OnClick.AddListener(StartAttack);
}
private void StartAttack()
{
anim.SetBool(P_ATTACKING, true);
}
private void OnMouseUpAsButton()
{
anim.SetBool(P_ATTACKING, false);
}
This might not work with multiple buttons but you could probably hack it to make it work. Just check which button was pressed via a custom tag .
Nice, just as a re$$anonymous$$der for all the newbies out there :)
FixedUpdate => for "Get Input" like $$anonymous$$ovement Update => for "GetDown or GetUp Input like attack, jump, or whatever
Crud... I just saw a bug... The ! after else if... I fixed that and it didn't work, though. I then copied and pasted your code, and the code seems to become true on GetButtonUp. I think this has something to do with it being UI, though.
oh I re-read your question. I will change my answer.
Thanks! I found a small bug in your code. OnClick() should be onClick(). Thing is, now the animator's bool doesn't switch back. Will keep digging.