Randomize Enemy Spawn Location Between Either Side of Screen

Eric Veciana
2 min readDec 14, 2022

--

Objective: Have your enemy spawn from either the left or right side of the screen

First, lets create a starting point for our enemy. Since I want my enemy to spawn either on the left side or the right side, I created a variable that will generate between a 0 and 1. Then, depending on the number generated, the enemy will start on either side of the screen.

Next, we will implement our Sin wave function and the full Start method code will look like this.

Now, within the Update method, we will have that first randomly generated value determine which direction we will move. Be sure to declare either a -1 or a 1 to the initial _direction variable for this to work.

Next, place the _direction variable within the transform.Translate function. This will make the enemy move left or move left depending on the value given.

Next, I will create the enemy fire function for this enemy. Stay tuned!

This game is now playable on itch.io. I update it as frequently as possible. Give it a play and let me know what you think!

--

--

Eric Veciana

As a Unity and C# developer with a background in nursing, I'm a creative problem-solver with a passion for creating games and other interactive experiences.