Adding a Thruster Boost

Christian Carter
2 min readJun 14, 2021

--

This tutorial covers adding a thruster boost to increase speed while holding down the left shift key. This can all be done inside the Player script. First add two variables, one to track the size of the thruster boost and another to point the thruster object.

private float _thrusterBoost = 1.0f;
private GameObject _thruster;

Then in the start function find the thruster game object and assign it to the variable. Don’t forget to verify it was assigned.

_thruster = GameObject.Find("Thruster");
if (_thruster == null)
{
Debug.LogError("The Thruster is NULL.");
}

Next, create a fuction to check if the thruster is getting a boost. In the function check if the left shift key is pressed down. If it is increase the thruster boost. Also, to show the effect to the player you increase the size of the thruster by adding a new Vedctor3 to the transform.localScale. Then in another if statement check if the left shift key was released. If it was then reset your thruster boost and the thruster scale.

void CheckThrusterBoost()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
_thrusterBoost = 2.0f;
_thruster.transform.localScale += new Vector3(0.2f, 0, 0);
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
_thrusterBoost = 1.0f;
_thruster.transform.localScale -= new Vector3(0.2f, 0, 0);
}
}

Last, in your CalculateMovement function call your new function to check the thruster boost. Also, make sure you include thruster boost in your calculation of the speed.

CheckThrusterBoost();
transform.Translate(direction * _speed * _thrusterBoost *
Time.deltaTime);

Now the player can boost all around the screen anytime they want by pressing the left shift key.

Good Luck Adventurer!

-Chris

--

--

No responses yet