- Home /
How to add double taping and button combination to games.
Hi. In my game I want to make button combinations for different fighting combos and double tapping buttons for things like sprinting. How can I do this and also make sure that if the buttons are not pressed within a second of each other, they will do individual moves?
Thanks for reading this,
-SK
Answer by GiyomuGames · Oct 28, 2015 at 02:23 AM
I guess you need to handle each button press normally (otherwise you will have lag which is not pleasant for the player). However if another button is pressed very quickly (<.25 seconds?) then you cancel what you started doing and do whatever your combo / double tap is supposed to do.
Thanks for the answer but would you $$anonymous$$d elaborating on it?
Let's say you want to detect that the player has pressed the space bar twice in less than 0.1 seconds. You can have a variable bool spacePressedOnce and a variable spacePressedTime.
Then you do something like
if (Input.Get$$anonymous$$eyDown("space"))
{
if (spacePressedOnce && spacePressedTime - Time.time < .1f)
{
// here you do whatever you want to do when space if pressed twice in less than .1s
// We reset the spacePressedOnce variable
spacePressedOnce = false;
}
else
{
// This is your normal case where space is pressed once so you do the normal thing
// Set the spacePressedOnce variable and store the time it was pressed
spacePressedOnce = true;
spacePressedTime = Time.time;
}
}
Now this would only work for this specific and simple case. For more complicated combos you'd need a much smarter approach, probably using a graph similar to the animator graph. I have never tried and can't really help for that sorry.
Answer by arcifus · Oct 28, 2015 at 01:07 PM
I wouldn't re-invent the input handling wheel for such a game. There are some nice ready made assets on the asset store that will handle a lot of input combos for you. You can concentrate on other aspects of your game instead.
Here is one such package: https://www.assetstore.unity3d.com/en/#!/content/3283
Your answer
![](https://koobas.hobune.stream/wayback/20220612043010im_/https://answers.unity.com/themes/thub/images/avi.jpg)