Adding Enemies Into Space Shooter

4 min readMar 26, 2021


I’ll start by creating: An enemy Game object placeholder (Cube) with its own material and script then create a prefab out of it. The script will be given a name ,’Enemy’ and it will be responsible for handling the enemies' behavior in our game.

The enemy will have three basic behaviors, it should move, detect collisions between lasers or the player, and to ,’Respawn’ back on top.

I want the enemy to constantly move down in the Y axis and reappear back to the top of the screen when its not destroyed. I would rather reuse the same game object than calling Destroy() and Instantiate() again. Doing this will help with performance down the road as its make much less computations.

I've created two functions called Emovement and Eboundaries which are placed within update() to be called per frame. For movement, ive simply written the shorthand version of Vector3(0,-1,0) and multiplied it by an adjustable float variable and time.deltatime. Time.deltatime so that this object will be moving per second rather than per frame.

Boundaries will check if the object is less than -5.0 and will tranform the position of the cube in a random X position and back up to 8.30 if the statement returns true. This is how it looks

next would be establishing collisions and damage to the player.

I decided on only attaching a rigidbody to the enemy over attaching it to the bullets because I don't want my scene over populated with objects that has rigid bodies on them. Bullets will predictably spawn more than the enemies as the current gameplay mechanics conditions you to mash the spacebar key.

The rigidbodies in this scene wont be calculating any physics whatsoever which wont impact performance at all. They’re there to detect colliders or in this case Triggers.

Ill flip the is Trigger checkbox of the laser and enemy and begin filling in the enemy script’s OnTriggerEnter() to check if its collided with the lasers or the player.

I confess that I went on a tangent and did more than i stated above.

I went from checking if the colliding objects tag (other) was equal to “Laser” or “Player” then executing a simple destroy to adding enemy damage and functional health to the player.

what's occurring here is a created a reference variable to store my Player component to both access my public Damage method. I want to check if the player object has the player script attached to it before trying to call the Damage method to catch any null reference errors.

The damage method within the player script is called from the enemy script when the enemy gameobject has collided with the player gameobject . The enemy script passes an adjustable integer to the function in which is then subtracted from a PlayerHealth integer variable. If PlayerHealth is less than zero, then we destroy the object. Game over.

On the other hand, the laser is much less complicated. It destroys the laser first and then the enemy. Here's the final result with 3 enemies.

I was hit twice and managed to take one out with a laser. As expected the player health decresed from 3 to 1.

Everything works out as it should and im happy with the results. Now its time to go into Gitbash to commit and push my current work into the respository.