Journal 142 — Unity Project Development, 2.5D Shooter
Objective: To add power ups, player weapon power ups, ability to destroy player and enemy ships, and a restart option.
To add the power ups, I will start with several prefabs that will upgrade the player’s weapons, and I’ll also have some pickups for additional coins, and a shield. I will only work with the weapon upgrade first to make sure it works.
I will add the upgrades to spawn randomly with each enemy ship. I will modify this later to have 1 or 2 enemies in a wave spawn, and have their ships flash or glow to indicate they hold a power up that will release a power up when they are destroyed.
In the Power Up Spawn script, I will check for the position of the enemy ship, that it is within the Game View, and then spawn a random power up.
In the Power Up script attached to each power up, I will have it move to the left of the screen towards the player, and I will also have it destroyed in 15 seconds.
Power Up Rewards
The next part of the power up is to check the collision and set up several functions to upgrade the player weapon, provide coins, and add a protective shield. For this first effort, I will fix the player weapons, and leave some Debug.Log statements on the other power ups. I will also set it if the enemy collides with a power up, the power up will be destroyed. In the collision, I will check the name of the power up.
In the Player script, I will have two functions to update the Weapon Index. To start, Index 0 is a single shot, 1 is double shot, and 2 is triple spread shot. I will set up one function to increment and one function to decrement the index when the player is hit.
In the Update method, when checking for the fire button, I will pass the Weapon Index to the WeaponFire script.
Let’s fix the player damage so that after increasing the weapon level, the player can also get damaged and lose weapon level, and ultimately be destroyed.
In the Player script Damage() method, I will call the WeaponDown function, check if the Weapon Level dropped below 0 and if so, destroy the player. Then I will reference the Game Manager for a game over sequence, which allows the user to restart the game. I have not updated the UI yet so I will keep this commented out. I also have not developed a death animation so I will keep this commented out as well.
In the base Enemy class, I will check the collision for the player. I have the method currently set up to check for a shield condition as well, but in essence, I’ll call the player Damage() function and destroy the enemy. I will also disable the enemy’s collider, as I ran into an issue on a similar game where multiple collisions got recorded.
After fixing the player damage, we can add the enemy damage behavior.
Game Over and Restart
If the player is damaged while at their base weapon level, they are destroyed. I added an option to press “R” to restart the game.
In the Game Manager script, I will check for the restart key, and also if gameover is true.
Restart and Waypoints
During testing, I ran into an issue where the waypoints were assigned correctly on the first playthrough, but after restarting, the set of waypoints were duplicated or more depending on the number of restarts. To address this, I used a counter to only allow the waypoints to be assigned once, and to clear the list of waypoint when the game is over. I also reformatted the Waypoints getter setter to use the getter to assign the waypoints only if the waypoints assignment had not been done.
Finally let’s see this in action.
Thank you for your time!
Next updates should cover the UI.