How to create a ‘sprint’ for the Player

Press shift for speed boost

Sometimes you need that extra bit of speed to dodge that bullet or grab that powerup. That’s why we will create a ‘sprint’-function today.

Slightly moving faster when going to the left. Might be a bit hard to see.

Pseudocode

If shift is pressed down
Then increase speed by multiplier
Else
Reassign initial speed

Normally this would work fine, but we already got a speed boost powerup in our game that multiplies our speed.

Final Sprint Code

A few minor adjustments to make it work along with our speed powerup. We don’t reassign _speed, because that would disable our powerup speed boost aswell. We can’t use just else now. Our speed would be divided by the modifier every Update(). This is why we use GetButtonUp.
Optional change is using the Unity Player Settings and using the named controls from there.

//Sprint
if (Input.GetButtonDown("Fire3"))
{
_speed *= _sprintModifier;
}
else if (Input.GetButtonUp("Fire3"))
{
_speed /= _sprintModifier;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tristan Engel

Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.