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

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Maximising RoI from available technologies

Why you should use CloudWatch Embedded Metric Format

Swift Delivery and Reliability is What DevOps’s Upshot!!!

Full stack .NET Development Guide

Using Exemplars in Helidon Metrics

java servlet learning

My journey to learn Python — Day 7

SOLID Principle

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.

More from Medium

2.5D Platformer

Digital Twin as Code (DTaC)

Understanding gcc main.c in C language.

my understanding of c++ inline