Problem to be solved :
Need to detect whether the player has pressed the specified key successionally to invoke 'running' animation.
Solution :
Record the time when the first and second time the key is pressed.
Check if their difference (in absolute) is smaller than 0.5
// successional key pressed detection viriable
float FirstPressTime = -100; // time for first press key
float SecondPressTime = 100; // time for second press key
private bool FirstPress = false; // bool variable used for successional key pressed detection
void WalkAndRun()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
bool onechance = false; // only one chance for each press, in case it records both first and second press
StartWalking(); // start walking on key down
if (FirstPress == false) //
{
FirstPressTime = Time.time;
FirstPress = true;
onechance = true;
Debug.Log("FP" + FirstPressTime);
}
if (FirstPress == true && onechance == false) {
SecondPressTime = Time.time;
FirstPress = false;
Debug.Log("SP" + SecondPressTime);
}
if (Mathf.Abs(SecondPressTime - FirstPressTime) < 0.5f)
StartRunning();
else
{ }
}
if (Input.GetKeyUp(KeyCode.RightArrow))
{
StopWalking();
StopRunning();
}
}