Day 56: New Basic Enemy Movement

Tyler Smallwood
2 min readJun 23, 2021

--

Objective: Get enemy to move down at an angle.

We are going to allow all enemies that are using the base Enemy classes movement the chance to go diagonal left, right, or just straight down like normal.

To start off let’s go ahead and create a global variable in the base Enemy class. This variable is a

Next is to give it a value from 0 to 2 when the enemy spawns into the game. In the Start function, let’s assign a Random.Range to _randomMovementSelect.

Remember, since we are using Int values in the random range, the max that is input is excluded.

Now to use that random movement select variable. In CalculateMovement function let’s add a Switch statement and input the _randomMovementSelect inside its condition.

Depending on the value from the _randomMovementSelect, if it’s one go diagonal to the right by .5f, if it’s two go left by .5f, if anything else go down like normal.

To make it more interesting, you could have the _randomMovementSelect recalculate after it moves below the correct y value. So everything an enemy is re-spawned at the top of the screen it could have a different movement. all that would need to be done to assign the _randomMovementSelect to Random.Range again.

To make the code cleaner you could create a function that does this calculation.

Thank you for reading! Have a wonderful day!

--

--

Tyler Smallwood
Tyler Smallwood

Written by Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer

No responses yet