Day 35 — Improvements: thruster UI element
Hey and welcome!
In this exciting installment we’re going to limit the usage of the thruster and provide some on screen representation when the thrusters are getting too hot and becoming unusable.
Let’s start by updating our Player script:
private float _thrusterAccelerate = 0.1f;
private float _thrusterDecelerate = 0.5f;
private float _nextThrust = -1f;private int _thrusterTotal = 0;if (_collectedSpeed == false && Input.GetKey(KeyCode.LeftShift) == false)
{
_thruster.SetActive(false);
transform.Translate(direction * _playerSpeed * Time.deltaTime);
if (_thrusterTotal > 0 && Time.time > _nextThrust)
{
_nextThrust = Time.time + _thrusterDecelerate;
_thrusterTotal -= 1;
_uiManager.UpdateThrusterText(_thrusterTotal);
}
}else if (_collectedSpeed == false &&Input.GetKey(KeyCode.LeftShift)){
if (_thrusterTotal < 50)
{
_thruster.SetActive(true);
transform.Translate(direction * _thrusterSpeed * Time.deltaTime);
if(Time.time > _nextThrust)
{
_nextThrust = Time.time + _thrusterAccelerate;
_thrusterTotal += 1;
_uiManager.UpdateThrusterText(_thrusterTotal);
}
}
else
{
_thruster.SetActive(false);
transform.Translate(direction * _playerSpeed * Time.deltaTime);
}
}
Quite a lot going on in here now so let’s start with the code that runs when the LeftShift is pressed. We start things off with checking if the _thrusterTotal value is less than 50, that number is going to be our cap, as soon as we hit that number the player will no longer get the speed boost which is why we have the else statement here that sets the speed to the _playerSpeed event with the LeftShift pressed.
Next we have ourselves another if statement if(Time.time > _nextThrust) it’s been a while but you’ll hopefully recognise this from our cooldown system we created in order to force a fire rate on our laser. As a brief rundown Time.time is the total time that our game has been running for so I’ve set our _nextThrust as -1f initially to guarantee that this if statement runs.
Once it runs we assign _nextThrust the value of the current time plus our thrusterAccelerate value which is 0.1f. So every 0.2 seconds roughly the _thrusterTotal increases by 1 until it reaches 50. Then we have a reference to the UI manager to update the thruster UI which we’ll implement shortly.
With that knowledge if (_thrusterTotal > 0 && Time.time > _nextThrust) is effectively doing the same thing except this time we’re taking 1 away from the thruster total and the decelerate has a higher value of 0.5f so that the thrusters cooldown slower than they heat up to stop the player from spamming it.
Now let’s create a UI object for our thruster text, create a variable for it in our UIManager script and then assign our new text object ot it:
Next let’s add in a new method into our UIManager script, I think you’ll like this one.
public void UpdateThrusterText(int thrusterTotal)
{
if (thrusterTotal <= 20)
{
_thrusterText.color = Color.green;
_thrusterText.text = " " + thrusterTotal;
}
else if (thrusterTotal > 20 && thrusterTotal < 40)
{
_thrusterText.color = Color.yellow;
_thrusterText.text = " " + thrusterTotal;
}
else if (thrusterTotal >= 40)
{
_thrusterText.color = Color.red;
_thrusterText.text = " " + thrusterTotal;
}
}
Remember our colour changing capabilities with our shield? Well text has a Color property too so you can bet that we can change that in code too!
Our thrusterTotal value is passed in from our Player script, when it’s less than or equal to 20 we set the text color to green, between 20 and 40 the text is yellow, and finally for the last 10 the text is red and looks like this in action:
Pretty cool stuff right? And this was all done using stuff we’ve learned before!