Implementing Thruster System
Objective: Learn how to implement a thruster system for the player.
So we have our game working, but we noticed that our player movement is quite slow. We already have a way to increase our speed with the Speed Booster Powerup. But how about we give the player some thrusters ?
They can give the player some speed boost while they hold down the Left Shift key, and are turned off when released.
So how do we implement this ? Well, we are going to need a new variable to multiply the player speed. It would also make it easier for us to have a way to check if the player have the key pressed.
private float _thrusterSpeedMultipler = 1.4f;
private bool _thrusterActive = false;
private void Update(
{
if (Input.GetKey(KeyCode.LeftShift))
{
_thrusterActive = true;
_thruster.SetActive(true);
}
else
{
_thrusterActive = false;
_thruster.SetActive(false);
}
}
So let’s check everything we are doing in the code above. So we have our multiplier for our thruster system and we have a bool to check when the system is active.
By using Input.GetKey(KeyCode.LeftShift) we are telling Unity that whenever the player holds down the LeftShift, we are going to activate the Thruster.
We are always checking if the player presses the LeftShift inside an if statement. Our Input method returns a bool, which means that our else is called whenever the player releases the LeftShift.
So when the player holds it, we just activate the thruster object and set our bool for the ThrusterActive to true. And when the player releases it, we just turn them off.
private void MovePlayer(Vector3 direction)
{
transform.Translate(_playerSpeed * _thrusterSpeedMultipler * Time.deltaTime * direction);
}

So we have our Thruster System working, but now we have integrate it with our currently working Speed Booster System, which is activated when our player collects a Speed Powerup.
private IEnumerator EnableSpeedRoutine()
{
_powerUpSpeedMultiplier = 2.5f;
_thruster.SetActive(true);
yield return new WaitForSeconds(5f);
_powerUpSpeedMultiplier = 1f;
_thruster.SetActive(false);
}
This our Coroutine that is activated when the player collects the powerup.
We have to make sure that when the player has collected the powerup, he will be faster than when just using the Thruster System. We are also going to make sure they both don’t stack, otherwise we would give the player a crazy amount of movement speed.
private void MovePlayer(Vector3 direction)
{
if(_thrusterActive && _powerUpSpeedMultiplier == 1f)
transform.Translate(_playerSpeed * _thrusterSpeedMultipler * Time.deltaTime * direction);
else
transform.Translate(_playerSpeed * _powerUpSpeedMultiplier * Time.deltaTime * direction);
}
These changes above check if the player has the Thruster System activated, and if the Speed Booster was not collected. In case both of these are true, we use our Thruster System, giving the player a 40% movement speed boost.
When both conditions are not met, we will always default to our else. This means that we will them simply use our PowerUpSpeedMultipler to increase the player movement speed.
As shown before, the value of our PowerUpSpeedMultipler is only changed when we collect our powerup, otherwise its value will be one. Why is that ?
Well, if the player is activating the Thruster System and them collects the powerup, we will go faster by using our PowerUpSpeedMultipler, which now is set to 2f. In case the player is not using the Thruster System, we always default to our else, which will move the player based on our PowerUpSpeedMultipler, which will be defaulted to 1f in case the Speed Boost is not active.
private void Update()
{
if (Input.GetKey(KeyCode.LeftShift))
{
_thrusterActive = true;
_thruster.SetActive(true);
}
else
{
_thrusterActive = false;
if (_powerUpSpeedMultiplier == 1f)
_thruster.SetActive(false);
}
}
Now with this small update to our else statement, we make sure that if the player collected the Speed Powerup, if he is not holding the LeftShift key, the Thruster for the player will not be deactivate while the PowerUpSpeedMultiplier is higher than one. We do this to make sure our thruster is always shown during the boost.
As soon as this returns false, which means that our boost has ended, we turn off the thruster object.
And there you have it ! We now have a working Thruster System for our player, which can be used to slightly increase his movement speed!