Speed Boost Part 2

Understanding how we created this script.

In part one, we added the PowerUp Script to our Speed Boost Prefab. We need to open that up and we are going to create a private int. This is how it should look like when you are done.

[SerializeField]

private int powerupID;

Then you may need to put a note below using /* Then placing the correlating numbers and what it is. Like TripleShot = 0, SpeedBoost = 2; and Shields = 3. Then remember to close it off with */.

Now you will need to go to the OnTriggerEnter2d. Now, there is two ways of doing this, first way if you want you can write an If /else statement. Or the second way is using a switch statement. I choose to use the switch statement due it is a cleaner way of coding. So how does one write a switch statement? Well inside the OnTriggerEnter2D, under the first if statement, you will write

Switch (powerupID )

{

Case 0: (Side note: Notice the change of punctuation. It is using a colon instead of a semicolon.)

player.TripleShotActive();

break; < This is used to stop the active code to run.

Case 1:

Debug.Log(“Speed Boost is working”);

break;

Case 2:

Debug.Log(“Shields are working”);

Break;

Default: < (This allows to return to default state and it will throw a debug if it comes that letting us know that there is a misstep.)

Debug.Log(Default Log);

}

So now we have that done, we need to move to the Player script. Create a new private float and call it _speedMultiplier. So this will allow you to make the Speed Boost move faster than your set speed. So it should look like this:

private float _speedMultiplier = 2;

Now that I added a SerializeField and I will use it to help balance the speed. You don’t have to do it. Next, you will have to add a bool for the speed boost. This is how it will look:

private bool _isSpeedBoostActive == false;

Now we need to go to the function called CalculatingMovement. This is where we will start the first step. We need to find where we wrote the player’s speed.

Now we can use an if/else statement. It should look something like this:

if (_isSpeedBoostActive == false)

{

transform.Transslate(direction * _speed * Time.deltaTime);

}

Else

{

transform.Transslate(direction * _speed *_speedMulitiplier * Time.deltaTime);

}

Then you are going to start a new function writing it like below.

public void SpeedBoostActive()

{

_isSpeedBoostActive = true;

}

This will activate and allow it to run as pick up, now we need to create another process that will allow it to shut off after 5 seconds.

IEnumerator SpeedBoostPowerDownRountine()

{

yield return new WaitForSeconds(5.0f);

_isSpeedBoostActive = false;

}

With all this done, it should be working like a charm. You can do the same steps and creating the shields. So the next article will be about how I created a User Interface.