- Home /
Quick Attack/Heavy Attack on Same Input
I'm trying to set up a system in Unity that allows a player to click one to perform a quick attack, but also allows them to hold the same input down and use a heavy attack, here's my first attempt:
if ((Input.GetButton("Fire1")) && (Input.GetAxis("Horizontal") > 0))
{
holdTime ++;
if(Input.GetButtonUp("Fire1"))
{
if (holdTime >= 10)
{
rightHook();
}
else
{
rightJab();
}
}
}
So as you can see, the idea is to increase the "holdTime" while the button is being held, then on release it'll check whether or not the "holdTime" exceeds the threshold for a heavy attack. I've got a few guesses regarding why this isn't working, but no ideas on how to actually implement this sort of system. Any help here would be appreciated.
Answer by ShadyProductions · Aug 07, 2017 at 10:17 AM
I believe you need to check for
if(Input.GetButtonUp("Fire1"))
{
if (holdTime >= 10)
{
rightHook();
}
else
{
rightJab();
}
}
outside of the initial GetButton, because it will never be fired in GetButton. Also you will have to add Time.deltaTime to holdTime, otherwise you're just incrementing by frames.
Yeah I figured that as being the major issue, the problem there was discerning between a heavy and a quick attack. I've managed to get it working to some extent, albiet it's messy and I'm getting issues with the animations triggering more than once despite conditions that should stop more than one attack being carried out at once. Oh well here's the revised code anyway for some closure:
if ((Input.GetButton("Fire1")) && (Input.GetAxis("Horizontal") > 0))
{
holdTime++;
if (holdTime >= 10)
{
rightHook();
holdTime = 0;
}
}
if ((Input.GetButtonUp("Fire1")) && (Input.GetAxis("Horizontal") > 0) && holdTime < 10)
{
rightJab();
holdTime = 0;
}