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;
}
↧