Enemies Straight Ahead!

It’s raining enemies!

Day 6

Now that our Laser is working and balanced, we can add enemies to our game so our Player has something to fire at. Today we will create an Enemy GameObject and implement Enemy behavior.

Enemy GameObject

So the first step to creating our Enemy is to create a GameObject.

Does anyone remember why it’s blue?

Then we create an Enemy Material and add it to our Enemy GameObject.

Lastly, let’s turn the Enemy GameObject into an Enemy Prefab because we want multiple enemies to appear in our game.

That’s why the Enemy GameObject is blue!

Now we have an Enemy Prefab!

Enemy Behavior

Now that an Enemy has appeared, we want it to do something. Let’s implement Enemy Behavior by creating an Enemy Script.

Let’s spawn our enemy at the top so our Player has time to react and fire at the enemy.

  • To set a spawn point, we set the GameObject’s position in the Start function
  • The Start function is called right when the game starts

Now, the Enemy should move down towards the Player to give the Player a challenge.

The Speed variable is set to 4 for the enemy.

Alright, now the Enemy spawns at the top and goes down towards our Player. Let’s try it out.

Nice! It works, but after the Enemy leaves the screen, they just disappear forever. Let’s add a respawn mechanic so after the Enemy leaves the screen, they will respawn at the top.

Rundown of code:

  • if statement is checking when the Enemy leaves the screen by tracking the ‘y’ position of the GameObject
  • randomX variable is getting a random number between -9 and 9 so the Enemy can respawn at a random ‘x’ position on the screen
  • transform.position is setting the Enemy’s new position after it leaves the screen, using a random ‘x’ value and 8 for the ‘y’ value so the Enemy reappears at the top

Let’s test out our game!

Nice, it works! Our enemy now reappears at the top after it leaves the screen. Thanks for reading!




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Chapter 9 Actors in Groovy, Java, JRuby, and Scala

Upload local data to IBM Db2 Cloud

Running Selenium Web Tests ( Maven + Java ) with GitHub Actions

Github Actions

Cool Goldens

How I developed a laravel package that will generate all countries and states around the world…

Parsing with Google Gson library in Android Kotlin

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Christopher Chu

Christopher Chu

More from Medium

Player vs Enemy!

Connecting GitHub to Unity

2.5D Platform: Calling for an Elevator

Unity Tutorial — Toon Shade in Shader Graph