Creating a Thruster Cooldown System

Christian Carter
4 min readJun 23, 2021

--

This tutorial will show you how to add a cool down system to the thruster so that once the player uses it they need to wait for it to charge back up again. This will include adding a a display to the UI to show the status of the thruster.

To start add a text object to the UI Canvas. This will be a temporary way to display the thruster status while getting the cooldown system in place. Then when it is working it will be changed to a scaling bar. If you need a reminder on how to create UI elements you can find one here.

The next step is to setup the thruster cooldown system in the Player script. Start by adding a couple of variables to track the amount of charge for the thruster and if the thruster is charged. Also, add a variable to track if the thruster is active.

private float _thrusterCharge = 100;
private bool _isThrusterCharged = true;
private bool _isThrusterActive = false;

In the UI manager add a variable for the thruster text object and assign it in the Unity editor. Then add a function to update the text of the object. This will be called by the player.

[SerializeField] private Text _thrusterText;public void UpdateThruster(float thrusterCharge)
{
_thrusterText.text = "Thruster: " + (int)thrusterCharge;
}

Next, return to the Player script. Now it is time to update the CheckThrusterBoost function that was added in the last tutorial. See below for the updated code. The key changes are that it is now tracking if the thruster is active and if it is then it is reducing the thruster charge. If the thruster is not active then it will charge the thruster. It also now limits the thruster to only be activated when the thruster is fully charged. It will also deactivate the thruster when the thruster is empty. At the end it calls the UI manager to update the text.

void CheckThrusterBoost()
{
if (Input.GetKeyDown(KeyCode.LeftShift) &&
_isThrusterCharged == true)
{
_thrusterBoost = 2.0f;
_thruster.transform.localScale = new Vector3(1.2f, 1f, 1f);
_isThrusterActive = true;
_isThrusterCharged = false;
}
if (Input.GetKeyUp(KeyCode.LeftShift) || _thrusterCharge <= 0)
{
_thrusterBoost = 1.0f;
_thruster.transform.localScale = new Vector3(1f, 1f, 1f);
_isThrusterActive = false;
}
if (_isThrusterActive == true)
{
_thrusterCharge -= 1f;
}
if (_isThrusterActive == false && _isThrusterCharged == false)
{
_thrusterCharge += 0.25f;
if (_thrusterCharge >= 100)
{
_isThrusterCharged = true;
}
}
_uiManager.UpdateThruster(_thrusterCharge);
}

Now that the thruster has a cooldown system it is time to update the display of how much of a charge the thruster has. The display for the thruster is going to use a Slider. Add a Slider to the Canvas. Delete the “Handle Slide Area” from the slider object in the hierarchy. Anchor the slider in the bottom right corner of the screen and adjust the size so it fits well. Now under the slider in the hierarchy go to “Fill Area” and under that open Fill in the Inspector. Adjust the color to something that looks cool.

Next, go back into the UIManager script. Create two variables, one to reference the slider and one to reference the slider fill rect. Assign the slider in the Unity editor to the variable.

[SerializeField] private Slider _thrusterSlider;
private RectTransform _thrusterSliderRect;

Inside the start function assign the thruster slider fill rect like this:

_thrusterSliderRect = _thrusterSlider.fillRect;

Last, update the UpdateThruster function to update the size of the fill rectangle by using SetInsetAndSizeFromParentEdge, You will pass this function three things. RectTransform.Edge.Left to tell it which side it is anchored from. “-5” so that it will have the proper offset. The width of the slider divided by 100 times your thruster charge so that the size of the fill rectangle matches the percentage of your charge. You can also remove the thruster text as it is now represented by the slider.

public void UpdateThruster(float thrusterCharge)
{
_thrusterSliderRect.SetInsetAndSizeFromParentEdge(
RectTransform.Edge.Left, -5,
150f / 100f * thrusterCharge);
}

Now you have a functioning cooldown system for your thruster.

Good Luck Adventurer!

-Chris

--

--