- Home /
Glitchy Double Tap to Run
So I'm building a 2D action game, similar to Smash Brothers, so I need to have the characters be able to double-tap to run. I have coded it and it works, but not well. Often times the character will not respond to a double-tap when they should. Also, I put in code that should make it so you have to press the same direction both times, but if I hit left then right he will still start running (assuming he runs at all)
Here is the relevant code:
//check for running
if ((Input.GetButtonDown("Left") || Input.GetButtonDown("Right")) && isGrounded)
{
if (Time.time - runPress < runDelay)
{
if (Input.GetButtonDown("Left") && !isFacingRight)
isRunning = true;
else if (Input.GetButtonDown("Right") && isFacingRight)
isRunning = true;
}
else
{
runPress = Time.time;
}
}
if (isRunning)
{
speed = runSpeed;
}
else
{
speed = walkSpeed;
}
//read movement
if (Input.GetButton("Left"))
{
moveDirection = Vector2(-1,moveDirection.y);
isFacingRight = false;
}
else if (Input.GetButton("Right"))
{
moveDirection = Vector2(1,moveDirection.y);
isFacingRight = true;
}
else
{
moveDirection = Vector2(0,moveDirection.y);
isRunning = false;
}
What's the value of "runDelay" ? Perhaps it needs to be adjusted?
Answer by IgorAherne · Aug 14, 2013 at 07:09 PM
float currTime = 0;
bool firstPress = false;
bool skipNext = false;
void Update(){
if(Input.GetButtonDown("Left") && ! firstPress){
currTime = Time.time;
firstPress = true;
skipNext = true;
}
else
skipNext = false;
if(input.GetButtonDown("Left") && firstPress && currTime + 0.5 > Time.time && ! skipNext)
/*start running */
firstPress = false;
}
else if( ! skipNext)
firstPress = false;
if(currTime + 0.5 < Time.time){
firstPress = false;
skipNext = false;
}
//now double tapping will work. Remember, Input doesn't happen during the update. It happens before. And you won't be able to do two taps before the update, so work around it like that. It can't happen all in one frame, but can be done some time after such frame, during another frame.
Please check this as well :) http://forum.unity3d.com/threads/191923-Full-corse-how-to-make-a-horror-game-AVAILABLE
Your answer
![](https://koobas.hobune.stream/wayback/20220613112220im_/https://answers.unity.com/themes/thub/images/avi.jpg)