New Enemy Movement (2D Space Shooter)

Alberto Garcia
2 min readFeb 25, 2023

--

This note explores a simple method to give movement to an object, in this case, the basic enemy moving downwards on the player’s field.

To enhance the enemy combat with a second enemy, a zigzag movement will be added. The common enemy has a broader X-axis spawn position, tackling often the sides of the map. This new enemy will be part of a group of coordinated attacks and its wavy movement will make it much harder to shoot at. To add an X movement along its built Y behavior, it will simply use Cosine to generate its float value.

Cosine works on the X axis, while Sine on the Y

Through the Mathf.Cos() function we can use time.time to generate the cycling value from 1 to -1. To tweak this values to a broader range of movements, the floats for frequency and magnitude alter the speed and the width of the curves.

[SerializeField]
private float _movementFrequency = 1.5f;
[SerializeField]
private float _movementMagnitude = 6f;

void CalculateMovement()
{
float x = Mathf.Cos(Time.time * _movementFrequency) * _movementMagnitude;
float y = transform.position.y - (_speed * Time.deltaTime);
float z = transform.position.z;

transform.position = new Vector3(x, y, z);
}

As simple as that, the new enemy can coordinate an attack, covering the center of our screen.

A picture of three spaceships dancing coordinately through the center of screen

To make these enemies difficulty a bit more threatening and significant, it’s attack rate is faster than the first enemy, making it a priority in combat. It’s darker color resembles the hasty and stealthy attacks of ninjas. Fast and Smooth.

--

--