Why does your dash ignore some presses?

Day 1 of 7: build a dash.

Why does your dash ignore some presses?

The problem

A press too early is thrown away.

void Update(){    if ( Input.GetKeyDown( KeyCode.LeftShift ) && canDash )        Dash();}

GetKeyDown is true for one frame. Press a moment before canDash turns true and the press is gone.

The fix

Remember when it was pressed.

Store the time of the press. Dash when you are ready, if the press is still fresh.

pressedAt = Time.time→dash within 0.15 s

The fix in code

An early press still counts.

float pressedAt = -1f; void Update(){    if ( Input.GetKeyDown( KeyCode.LeftShift ) )        pressedAt = Time.time;    if ( canDash && Time.time - pressedAt < 0.15f )    {        pressedAt = -1f;        Dash();    }}

0.15 seconds is a common buffer. Reset pressedAt so one press is one dash.

Watch out

Tune the window.

Too short and presses still drop. Too long and the dash fires when nobody asked.

0.1 s
tight, for fast games
0.15 s
a good start
0.3 s
feels like lag

The rule

Buffer the press, not the dash.

Store
the time of the press
Check
Time.time - pressedAt
Reset
after the dash