Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences…

Follow publication

Enemy From Different Angles- Game Dev Series 39

Objective: Create different enemy movement.

Feeling too easy about our game? It might be a good time to raise up the difficulty.

We can change the movement of enemy to make it harder to be shot.

I have got some ideas about the enemy movement. It will moves in 2 way.

  1. Normally go straight down.
  2. Move diagonal to the right or left randomly.
    When the enemy is out of scene area, teleport to the opposite on x-axis.

We will not choose which movement that the enemy has. Just make it random.

Open the Enemy script and create an integer variable as movement ID.

Since we have 2 types of movement, we can make it choose 1 randomly in Start().

In the Update(), we can use switch statement to definite the way of movement by ID.

Then we can extract the original movement into NormalMovement(), and create a new one: DiagonalMovement().

Within the DiagonalMovement(), we will have 2 ways of movement: To the left & right. The only difference is on x-axis, one should be negative integer.

We can simply change the random ID to control the x-axis.

And also some little changes on switch statement.

In this switch statement, 1 & -1 will be sent to DiagonalMovement() method. Means we need to use the ID to our method too.

Then we need to add some lines of code to make it flies back when out of boundaries.

These lines used to be in the movement method, now should be a good time to extract as new method.
Then we need to called this method both in NormalMovement() and DiagonalMovement().

All done! Now your enemy should be more tricky to hit!

Sign up to discover human stories that deepen your understanding of the world.

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

S.J. Jason Liu
S.J. Jason Liu

Written by S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.

No responses yet

Write a response