New Enemy Movement Behavior

Bradley Yachimowski
2 min readJan 10, 2022


One of the GameDevHQ 2D Game Course is to create a new Enemy Movement Behavior. In the current enemy movement behavior, we instantiate a new enemy prefab at a random location on the X-axis just above the game window and it moves in a straight line down the game window.

Now let's create a downward zig-zag movement. to do this we have to oscillate the X-axis so it moves left and right as it tracks downward. To do that we are going to switch between two transform.Translate statements.

The following are the variables needed to support our code’

And let's add the code:

Our new enemy movement code is located in the “FixedUpdate()” method. This keeps our movement somewhat constant regardless of our game's frame rate. We start counting up by using the ++ or increment function to increase the value of _countValue by 1 with an x-axis value of 1f which causes the enemy to drift to the right. When our _countValue becomes greater than the _xAxisMaxCountValue we set the _countUp variable to false and the next the if(_countUp==false) executes and _xAxis is set to a -1f which causes our enemy to drift to the left.

The transform.Translate() function applies the translation or movment is applied relative to the transform’s local axes. We can vary the _xAxisSpeed to change the amount of side to side swing. By varing the downward (y axis) _speed and the xAxisSpeed we can create interesting results.