New Enemy Movement Implementation

Jason Schneider
2 min readOct 6, 2021

Having our enemy only move straight down the screen can be challenging for the player, however, why don’t we step it up a notch. For this first change of enemy movement, how about we have the enemy move randomly left or right while still moving down the screen, for a random amount of time.

Commenting out the original code in our EnemyMovement method of the Enemy script and adding the below:

Then saving the script and jumping back into Unity and pressing play will give:

Ok, great, now that we have a basic alternate movement sorted out, let’s now sort out how to make them also move down the screen again and in the opposite direction as well. This; to me; seems like the perfect time for another Coroutine.

_dirChange can only equal -1 or 1

In this Coroutine we have introduced 2 new variables _altMovement and _dirChange, since our script has no idea what these variables are; we, of course; need to declare them.

In the Start method of our script we need to put:

Now, when we put the alternate enemy movement code; with the new _dirChange variable added; into an if else statement like:

Then saving the script and jumping back into Unity and pressing play will give:

--

--