Introduction
In the previous post, we created the game object for our invincible power up, now it’s time to integrate it into our game!
If this seems repetitive, then you’re correct! But we’re going to have to go through the same step anyways just so we’re all on the same page, however my goal in today’s post is to breeze through everything that we must do (especially since this will be the 3rd time!)
Today’s goal is to:
- Create the code that will pick up our invincible power-up
- Add it in our game
See? Nothing new. Either way, let’s get to it!
Step 1: Picking Up the Invincible Power-Up
Step 1.1: Setting up the Game Object
There are 2 things we need to do first:
- Add a Box Collider
- Add a tag
The first thing we’re going to do is we need to add a collider to our rocket
game object, so we can collide against it:
- Go to the
rocket
game object and add a Box Collider Because the size of our rocket was originally large, our Box Collider needs to be just as large to be able to contain it. Change the Size
to be (200, 200, 2000)
and the Center
to be (0, 0, 400)
. - Go to inspector and create a new tag called
Invincible
and set rocket
to have that tag.
When you’re done, we should have something like this:
Step 1.2: Updating PlaneCollider to Collide with the New Power-up
We should all know the drill at this point, we’re going to modify our PlaneCollider
to be able to detect and use the Invincible
power-up.
We’re going to have to create and update three scripts:
- Make a new
invincible
script for our rocket
game object - Add a new
Invincible
power-up type inside our PlayerManager
- Update
PlaneCollider
to use our new Invincible
power-up
Because this is a repeat of Day 90, I’m going to speed through the post here. Many things have already been explained to death, so I’ll just leave the scripts here.
Update our PlaneCollider
script:
using UnityEngine;
public class PlaneCollider : MonoBehaviour
{
…
void OnTriggerEnter(Collider other)
{
print(other + " name " + other.name);
switch (other.tag) {
case "Coin":
CoinCollision(other);
break;
case "Magnet":
MagnetCollision(other);
break;
case "Score":
ScoreColllision(other);
break;
case "Invincible":
InvincibleCollision(other);
break;
default:
CheckUnTaggedCollision(other);
break;
}
}
private void InvincibleCollision(Collider other) {
PlayerManager.Instance.AddPowerUp(PlayerManager.PowerUpType.Invincible);
Invincible score = other.GetComponent<Invincible>();
score.Collect();
}
…
}
Walking Through the Code
- Just like the other 2 power-ups already, whenever we collide against an object, we call
OnTriggerEnter()
which we changed to look for a game object with our new Invincible
. If we find it, we’ll call our new InvincibleCollision()
. - In
InvincibleCollision()
, we add our new power-up to our PlayerManager
script to manage it (with our new Invincible PowerUpType
that we still need to implement) and then grab the Invincible
script component from the object (which we still need to add) and then call its Collect
function.
Setting Up the Script
We already added the Invincible
tag and a Box Collider
to our rocket
game component so we don’t have anything we need to do here.
Step 1.3: Create Our New Invincible Script
Next up, we need to make our Invincible script that will deal with the aftermath of us colliding with the rocket
game object.
- In our
rocket
game object, create a new script called Invincible
.
Here’s what it looks like:
using UnityEngine;
using System.Collections;
public class Invincible : MonoBehaviour
{
public void Collect()
{
StartCoroutine(RemoveGameObject());
}
private IEnumerator RemoveGameObject()
{
yield return new WaitForSeconds(0.1f);
Destroy(gameObject);
}
}
I’m not going to walk through this as it’s the same as our other power-ups. In reality, what I really should have done was made a general script that I can attach to everything.
However, these are all potential refactoring topics that we could get into in the future. For now, I’m just going to leave it as is.
Step 1.4: Add Invincible as a type to the PowerUpType enum in PlayerManager
The last thing we need to do is update our PowerUpType enum
in PlayerManager
to include the existence of our new Invincible
power-up.
using UnityEngine;
using System.Collections.Generic;
public class PlayerManager : MonoBehaviour
{
public static PlayerManager Instance;
public enum PlayerState { Alive, Dead }
public GameObject Player;
public GameObject MagnetCollider;
public enum PowerUpType { Invincible, Magnet, Score }
private Dictionary<PowerUpType, PowerUp> powerUpDictionary;
private float powerUpDuration = 45f;
private List<PowerUpType> itemsToRemove;
…
}
Step 2: Spawn Our Power-Up in the Game
Step 2.1: Add a Container and Create a rocket prefab
At this point, our game
object is almost complete and ready to be used. One more problem that we haven’t addressed is that when we instantiate a game
object, we could change its rotation.
To fix this, we’re going to put our rocket
component to be inside an empty container and we can make a prefab out of that container.
- Create a new empty
game
object called Rocket
- Drag our
rocket
game object to be a child of our Rocket
game object. - Drag the
Rocket
game object into our Prefab
folder to create a prefab of it.
We can remove the existing game object off of our game hierarchy now, however like the 2 other power-ups, I chose to keep them in, so I can easily pick them up and test their behaviors.
Step 2.2: Add our rocket prefab to our ItemLoaderManager
Now that we have the prefab of our final power-up, we can add it to our ItemLoaderManager
script that we store in the Manager
game object.
ItemLoaderManager
is a script that contains all the power-up and items that our paths will use to randomly generate.
- In the
ItemLoaderManager
, change the Size
of our Power Ups
to be 3
. - Add our
rocket
prefab into Element 2
Here’s what our script looks like:
And that’s it! Now if we play the game, the power-up will show up!
Here’s what you might find out in the wild!
End of Day 94
That’s it! Hopefully at this point, there really isn’t anything new, most of what we have done here has already been done before.
Now that we have our invincible boost power-up into the game, in the next post, we’re going to the fun part and implement power-up!
I have a feeling that this will be the toughest one to implement! But let’s see what happens in the next post!