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