Feature: Thruster Boost

This article looks at adding a short-duration speed boost to the Player that will be activated with the Left Shift key and displayed as an energy bar on the screen.

To do this, we need to add some new variables to the Player script. We need a float to hold the Player’s normal or base speed to be changed back to it when the boost is over, a float to hold the amount by which the temporary speed boost or thrusters will increase the speed. Another float to determine how long you can use the thrusters for, and a final float to that will keep track of the amount of time you used the thruster, so we know when the duration of the thruster has been reached.

We need two bools, the first so we know when the thruster is active, and the second so we know when the thruster energy has been used entirely and needs to regenerate back before use again.

In Start, set the base speed to the Player’s speed variable.

To get the thruster feature working, we first need to make an if statement to check for the Left Shift key to be pressed down and check to make sure that the thruster is not down. If so, increase speed by the thruster boost and set the thruster to active. Also, we need to create an if statement that checks when the player releases the Left Shift key so that the speed will be set back to the base speed, so the thruster can be set as not active.

Next to make it so the thruster only lasts for 5-seconds of constant use. The first thing to do is add the time since the last frame to the elapsed time. Then create an if statement that checks if the thruster is active. If so, make another if statement to check if the elapsed time is greater than the thruster burn length. If it is, then make elapsed time the same as the burn length, set it so the thruster is down and can’t be used, deactivate the thruster so it will start to regenerate, and set the speed back to normal.

Then add an else if statement that checks if the thruster is not active and if the elapsed time is greater than 0. If so, subtract the time from the last frame from elapsed time, then check if the elapsed time is less than 0; if it is, set it to 0 and thruster down to false.

With that, the Player can now hold down the Left Shift key to increase their speed for 5-seconds straight before it has to recharge to full to be used again.

To give a visualization of Thruster Energy, we will use a UI Slider. To create a slider, you could use the premade one under UI, but I will use two images and apply the Slider script to one of them.

To start getting the Slider working, we need to reference it, so I created one in the UIManager script that we will then use to show the thruster energy.

Now to create a method to update the thruster slider. To be used by other scripts, the method needs to be public, and the arguments that we need to pass in are the elapsed time the thruster has been active and the maximum amount of time the thruster can be functional. Before changing the slider to visualize the thruster usage, the slider’s maximum value needs to be checked if it is the same as the burn length, and if not, set it to the burn length so that Slider will correctly so the progression from 5 to 0 instead of 1 to 0. Then change the current fill value of the thruster slider to the elapsed time subtracted from the burn length.

In the Player script, we now need to call the method in Start so that the thruster energy slider will be full, passing in the elapsed time of 0 and the thruster burn length.

Call the UpdateThrusterBar method in both the if and else if statements passing in the changed elapsed time, and the thruster burn length. That will change the slider whenever the thruster is used or regenerating.

In Unity, hook up the Thruster Slider to the Slider field on the UIManager script on the Canvas.

With that, we now have a working thruster system and a UI element to visualize it.




Unity Developer, Software Engineer, Game Developer

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

Recommended from Medium

CS371P Spring 2022: Antoney Ejimadu

How a Simple Statistic Law Can Help Detecting Fraud With AWS and Python

API Spotlight: Call Recording

Pathway to Mainnet v2.0–2021 Q1 roadmap

Creating SSL for a cPanel Addon Domain with GoDaddy

How to scrape the web with Python

Android Threads & Coroutines for Beginners

Best way to read specific columns from CSV in Pandas

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
Kyle W. Powers

Kyle W. Powers

Unity Developer, Software Engineer, Game Developer

More from Medium

Saving Data with Unity Part 2!

O of N Log N method

Gamedev Log: “Slash’n’crack” #1 (Unity/C#)

Introduction to C Programming