Creating Different Enemy Movement — Spaceship Game Made with Unity
Giving more troubles to the player
Objective: Create 2 extra different movements for enemies. Wave movement and diagonal movement.
First I created a variable called “_movementType” to represent what type of movement the enemy is gonna have and generate a random number in the start function to define the value for the enemy when it is instantiated.
In the Update function I add a switch case to define what happened in each case. In the default case the enemy will move in a straight line.
For case 1, this is gonna be the code for wave movement. The y value is -1 that means the enemy always goes down and the x value is calculated with the cos function that iterate the value between 1 and -1 multiplied by the values that we add, this is the key to achieve the wave movement.
The wave movement is ready.
Now in case 2 for the diagonal movement, in the start function we have to change the rotation of the game object and in the update function within the switch case the are gonna keep the same code than the straight line but multiply the speed by 2 to make the enemy faster.
Diagonal movement is working now.
This is how the update function looks completed with all cases.
And that’s it! We have enemies with different movements in the game.