Adding New Power Ups to The Game
The Speed Boost And Shield Power Up
Creating the Collectible Game Objects
The process is the same for both power ups. I created the Speed Boost power up and then repeated the same steps to create the Shield Power Up.
Create the the Game Object and animation.
The only difference between creating the collectibles is the name and the sprites that they used.
Add the Components
Each collectable gets its own Game Event for being collected. All other Components are the same.
I forgot to copy and past the transform values so the collectable was a little big. I had to change the scale to 0.5 on all axis.
Create the Game event that should fire for collecting the collectable and assign it.
Add the collectable to the prefabs folder.
Create the Spawners
Create a Spawner so that we can spawn the collectables.
Set the correct prefab to use for your spawner. Chang the player lives to use a variable and set it to the Player Lives Variable Scriptable Object, so the spawner stops spawning when the play dies. Set the screen Bounds to the Screen Bounds Scriptable Object. And set the Game Move Direction so the spawner spawns the power up in the correct location.
Add the spawner to the screen manager and make sure to add the container that we should set the spawned object’s parent.
Since I edited the Spawn Manager that was in the Scene and not the Prefab, I have to apply the changes to the that I made to the Prefab.
The game is now spawning our new power up collectables.
The Player Behavior
We can collect the power up but there is no functionality and they do nothing for our game play.
Implement the Speed Boost Power up
I added the Range attribute to the boost speed to keep the minimum to 1, the maximum isn’t that important here. Since this value will be multiplied in the move calculation if it is set to 0 this will cause the player not to move to prevent that I made it so you have to set it to at least 1.
There are several different ways to implement this. I could add a new Scriptable Object Variable for the current speed and access this new variable in the Moveable Behavior. I am already modifying the move direction in the Player Behavior so I am going to add the speed increase here.
Lets Test This Out.
Nothing Happened it appears that my new code is not working for some reason.
Actually the code does work correctly the problem is that we never told the player to respond to the Speed Boost Power Up Collected Game Event. I have to add a new game event listener to the Player to listen for and respond to the Speed Boost Power Up Collected Event.
Game Event Listener Requirements
I know that every time that I create a new power up that I need to make the Player to respond to that power up being collected. Instead of having to add the Game Event Listener Game Component to the Player Game Object for every power up I added a Game Event Listener and the Game Event to the Player Behavior for each power up. In the Awake method I made sure to add the game event listener component, register the listener call back method, set the event and add the event listener.
To make sure there are not extra game event listeners I then removed the Game event Listeners from the Player prefab.
Now when we play the game the event listeners are automatically added.
The Shield Power Up
Set the Event on the player prefab.
Create the Shield Game Object
Create the Player Shield Game Object and make it animate.
Since the Player is on the default sorting layer with an order in layer of 1, I changed the shield’s order in layer to 2 so it will display on top of the player. I added the circle collider and made sure that is was bigger the the Player’s collider. I added the Damageable Component.
I created the shield as a prefab and then added it to the player. I also removed the Damagable Component from the shield.
Activating the Shields
I need a game object to set active in the scene. I made sure that in the Start method the shields aren’t active. Then in the Activate Shields method I set the shields to active.
I then added the shield in the Player script. Since I did all of my editing in the scene and not on the Player Prefab I also applied the changes to the Prefab.
Destroying the shields
The damage being done in to the player is already being handled by the Damageable Behavior. I need to know if there are shields, if they are active, and a game object to deactivate if they are. to know if the shields are active or not I check to see if there are shields and if the shield game object is currently active. In the Awake Method I set the has shields to if there is a shield game object or not.
Now in the Damage Method all I need to do is check if the shield active if it is then i set the shield game object to be disabled and return out of the method. The rest of the Damage will now only run if the shields are not active.
Testing the Shields Work.
Don’t forget to set the shields game object for the player in the inspector.
The Game Running
Turned the spawn manager back on and checked to see if everything is working.