Unity Dev: Moar Enemy!

Couple things to go over today. We’ve got a working Enemy object, but what’s the fun unless we’re getting overrun with rude alien forces to overcome? On top of that, our lasers are currently powerless to stop them! I’ve also found it kinda boring to just have these cubes floating through and endless void, so let’s put our great space battle in, well… space.

Space Cube Warfare

Spawning Enemies

Problem: One enemy just isn’t enough to really feel like a threat that you need to deal with. We need something that will regularly add a new enemy into our scene.

Solution: This is a pretty simple one to deal with. The most important part is answering the question of how to have a function get called on a regular basis.

We’ll use an IEnumerator to solve this exact problem. Before getting into that, we’ll need some variables to ensure we’re making our future selves happy with our foresight.

_enemySpawnRate can be adjusted easily though the Unity editor.
_isSpawningActive can be used in the future to turn spawning off in case of something like our Player dying.

The two GameObject variables are for easily referencing those objects as we’ll need them in when we go to actually spawn the Enemy objects.

In the above, so long as _isSpawningActive is true, we create a local variable that defines a Vector3 point just to the right of the screen and on a random Y-axis point within the viewable area.

In our Instantiate() function, we’re providing a couple things: The Prefab we want to instantiate, the position to instantiate it in, the object’s rotation, and the parent object’s transform.

Closing out our IEnumerator, we use WaitForSeconds() to define the length of time that our game will wait before checking to see if _isSpawningActive is still true and executing our code accordingly.

Space Battle has Consequences

To prepare to do damage to our alien invaders (and take some), we’ll need to add a Rigidbody component so that our game can register collisions and act on them. Be sure to uncheck “Use Gravity” in the Rigidbody and check “Is Trigger” in the Box Collider component.

We’ll need Tags on our Player and Laser objects to move forward. If you don’t have them, ensure the Player object has the tag “Player” and the Laser prefab has the tag “Laser”.

Player script:

  1. Add a new int variable called _lives and set it to whatever you’d like.
  2. Setup a function that’ll handle reducing our lives.

Here we’re simply reducing _lives by 1 whenever Damage() gets run. If our lives drop below 1, we run a function in our Spawn Manager to set our _isSpawningActive variable to false and stop any more enemies from spawning. After that, we’re done with our Player object because we died!

But how do we take damage? In this case, our Enemy is going to do damage TO us, and will RECEIVE damage from our lasers, so let’s handle all of this directly in our Enemy script.

Our enemies have a Rigidbody component on them which means we’ll be able to use collisions on them. We’ve also set the Box Collider to be a Trigger, which means that our objects aren’t going to “physically” collide with each other, but will “trigger” some code to run when the Box Collider of another object enters into the space of its own.

We can use Unity’s built-in OnTriggerEnter(Collider other) void which gives us access to information about the object that’s colliding with us. We have two main conditionals here and we’re using Unity’s CompareTag() method to check the tag of other which represents the object colliding with the Enemy.

If the colliding object is our Player, we access our Player script using GetComponent<Player>() and run the Damage() function we set up earlier to reduce the Player’s _lives counter. After we’ve done that, the alien offender’s ship goes kaboom because we have superior spaceships obviously.

On the other hand, if we fired a shot at them and the object colliding with the Enemy is tagged as “Laser”, we simply Destroy() that instance of our Laser (again, referencing other ) and then Destroy() this gameObject (which happens to be the Enemy because we’re inside the Enemy script).

Blast Off!

This is a space shooter, so why’s it taken so long to get to space? Set up a new Empty object in your scene and call it “Background”. Find a nice graphic to make it look like you’re in space and put it in a new folder called Sprites. Drag the image on top of your “Background” object and BAM! You’re in space. A new object will be childed under “Background” complete with a Sprite Renderer component. Use this object’s Inspector for adjustments if it’s not placed quite right.

On a quest to become a game developer. Still sometimes providing unsolicited advice about how to function in society.