Let’s add a limit to our usage of thrusters

Jordan Evans
Nerd For Tech
Published in
4 min readJun 11, 2021

Today we will look into how we can limit the usage of our thrusters so that the player can’t just stay at a boosted speed infinitely. As to how the implementation of the thrusters is done, we did so here.
First, we will need to create an object to have our thrusters work from:

From here, we can move our bar to whichever corner we want, as seen with the bar that is going to be our final one in the bottom left. Before we get to that state though, we will need to adjust a couple settings on it first:

We will first want to open up our new slider object in the hierarchy and turn off our slide area. Within the parent object, we will adjust our value to the desired amount. From there, we can go to our fill and background option and change it:

Now that we have our thruster bar set up, we can go into our UIManager script and start creating our code to start making our Thruster bar work from. To start, we will make ourselves a Slider variable and a bool to work with:

Our slider variable, we will attach our newly made slider to in the Unity Editor. From here, we will go into our void start and set the value of our thrust bar to 10. As we are using a slider, we need to add .value to the line of code before entering the value we desire:

From here, we will quickly go into our player script and adjust how our speed of the ship is determined. As we will be using script connections, we will simplify how we determine when to activate our thrusters boost or not. First, we will make a couple new floats and then set up their values in the Void start:

From here, we will create a new public void on the player script to have our _speed values be determined with. To make it work easier, we will create a simple switch statement to determine if the thrusters are active or not:

From here, we will quickly move back to our UIManager script and add a couple new voids to work with:

To start, we are telling unity that if we are not using our thrusters, we want to have it charge at a rate of 1 point per second. Else, we want to use it at a rate of 3 per second as long as our value is 0 or above. The ThrustEnabled portion will allow us to use our thrusters as long as the remaining value we have left is 3 or higher. We are still able to use the thrusters if we keep our shift key held down while it drops below 30%, but if we let go of it after it drops below 30% or it hits 0, the thrusters will not work again till we hit 30%.

The last portion that we need to adjust is with our speed variable and how we adjust our script to make it work properly:

What we have done is moved our 2 movement methods into an if else statement. What we are telling Unity is that if we are holding LShift, we want to have our ThrustEnabled on the UIManager along with the _speed variable, which in this case will have the thruster multiplier. Else, our speed will just be a regular rate.
Now that we have all of this set up, we can move back to our Unity editor and check to see if the thruster works as intended:

There we have it, we have managed to change it so that the thrusters we have added now have a limited usage period so that we can’t just fly at a high speed all game long.

--

--