Brave the Expanse: Project Update 2

Erin Kerr
3 min readOct 10, 2022

Adding enemies to my top-down space shooter

Sample gameplay from last update

As of my last project update, Brave the Expanse only consisted of a movable player prototype that can fire projectiles. Let’s go over the recent changes:

Adding an enemy prototype

Sample of basic enemy prototype

With those projectiles, our player needs something to shoot. I added another basic cube object to the scene as an enemy prototype. The enemy moves down screen at a constant speed. Once below the bottom edge of the screen, the enemy relocates above the screen at a new random position from left to right, and continues to travel downward. At this rate, one enemy could live forever, so…

Adding enemy collisions

Sample of enemy collisions

Using Unity’s physics engine, I added Box Colliders to the Enemy, Laser and Player objects. Since all other objects (Player and Laser) would collide with the Enemy, I added the required Rigidbody component the Enemy to support collisions. I enabled “Is Trigger” on all colliders and added a unique Tag to each type of object as an easy way to differentiate them. Then, in the Enemy script, I added the OnTriggerEnter method to define the collision behaviors:

  • When the Enemy collides with a Laser, destroy both objects.
  • When the Enemy collides with the Player, destroy the Enemy object.

Adding player lives

Sample of player damage

To account for the Player taking damage upon colliding with an Enemy, I first added a integer variable to Player script to hold the player lives count, starting at 3. Next, I added a public method to the Player that reduces the lives count by one each time it’s called. It also checks if the player is out of lives and destroys the Player object if so. Then, to allow the Enemy to call this public Damage method, I updated the OnTriggerEnter method to get a reference to the Player script component from the Player. A future update will display the lives count on screen, but for now, the player will disappear after colliding with three enemies.

Adding a spawn manager

Sample of Spawn Manager behavior

To keep the game going without staging several enemy objects ahead of time, I added a Spawn Manager to control enemy spawn behavior. Using a coroutine, the spawn manager continuously spawns enemies as children of an Enemy Container object at random positions along the X-axis every five seconds. To stop spawning enemies when the player dies, I added a boolean switch to the Spawn Manager to track if enemies should be spawned and a public method to turn off this switch. Then I updated the Player’s Damage method to communicate with the Spawn Manager to stop spawning when the player is out of lives.

Next up: Assets!

--

--