Varying enemy movement in Unity

Objective: Provide new enemy movement based upon enemy type.

Enemies now have more diverse movement.

Enemies always moving down at the same speed is not realistic, so to add enemies with more variable movement the variables shown below are required in the Enemy script.

Variable for movement in Enemy script.

In the Enemy script, void Start() assign _spawnTime to the current time and _speed to to between 3 and 5 units per second. One quarter of the enemies are assigned an _enemyID of 1, which allows horizontal movement. A sine function will be used for the side to side motion, so the frequency and phase are assigned a random value as shown below, allowing for a range of enemy movement.

Assignment of variables in void Start() in Enemy script.

If _enemyID is one, a sin function provides the side to side oscillations in motion with variation in the magnitude or _speed, _frequency and phase of the sine wave within the void Update function.

Void Update() function in Enemy script allowing horizontal motion in some enemies.

These pieces of C# code allow for a broad range of enemy movement. Three quarters of the enemy still move down, but now with variable speed. A quarter of the enemy oscillate from side to side and wrap around.

Software engineer