Creating Dynamic Enemy Movement in Unity

Austin Hall
4 min readJul 24, 2023

--

Introduction:
In this tutorial, we’ll learn how to enable enemies in your Unity game to move in a new and engaging way. By default, enemies spawn at the top of the screen and move in a straight line towards the player. We’ll explore three different types of movements for enemies: side-to-side, circling, and coming into the play field at an angle. By following these step-by-step instructions, you’ll be able to add more variety and challenge to your game’s enemy behaviors.

Prerequisites:
1. Unity Game Engine installed (version 2019 or later).
2. Basic familiarity with Unity’s interface and scripting in C#.

1Implement Side-to-Side Movement

  1. Create a new C# script named “SideToSideMovement” and attach it to the enemy GameObject.

2. Inside the script, define variables for speed and movement range:

public class SideToSideMovement : MonoBehaviour
{
public float speed = 2f;
public float movementRange = 3f;
private Vector3 startPosition;
private float direction = 1f; // 1f for moving right, -1f for moving left
}

3. in the `Start()` method, store the initial position of the enemy:

void Start()
{
startPosition = transform.position;
}

4. In the `Update()` method, move the enemy back and forth between the defined range:

void Update()
{
float newPositionX = startPosition.x + direction * Mathf.PingPong(Time.time * speed, movementRange);
transform.position = new Vector3(newPositionX, transform.position.y, transform.position.z);
}

2 Implement Circular Movement.

  1. Create a new C# script named “CircularMovement” and attach it to the enemy GameObject.

2. Inside the script, define variables for the rotation speed and radius of the circular path:

public class CircularMovement : MonoBehaviour
{
public float rotationSpeed = 100f;
public float radius = 2f;
private Vector3 center;
private float angle = 0f;
}

3. In the `Start()` method, store the center position of the circular path:

void Start()
{
center = transform.position;
}

4. In the `Update()` method, make the enemy move in a circular path:

void Update()
{
angle += rotationSpeed * Time.deltaTime;
float newX = center.x + Mathf.Cos(angle) * radius;
float newY = center.y + Mathf.Sin(angle) * radius;
transform.position = new Vector3(newX, newY, transform.position.z);
}

3 Implement Angled Entry Movement

  1. Create a new C# script named “AngledEntryMovement” and attach it to an empty GameObject.

2. Inside the script, define variables for the speed and entry angle:

public class AngledEntryMovement : MonoBehaviour
{
public float speed = 5f;
public float entryAngle = 45f;
}

3. In the `Update()` method, make the enemy move along the defined entry angle:

void Update()
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
transform.Translate(Vector3.right * speed * Mathf.Tan(entryAngle * Mathf.Deg2Rad) * Time.deltaTime);
}

4 Implement Random Movement Selection

  1. Attach all three movement scripts to the enemy GameObject, but disable them initially.
  2. Inside the Enemy script, create an enumeration for different movement types:

4. In the `Start()` method, enable one of the movement scripts at random:

void Start()
{
selectedMovement = (MovementType)Random.Range(0, Enum.GetValues(typeof(MovementType)).Length);

switch (selectedMovement)
{
case MovementType.SideToSide:
GetComponent<SideToSideMovement>().enabled = true;
break;

case MovementType.Circular:
GetComponent<CircularMovement>().enabled = true;
break;

case MovementType.AngledEntry:
GetComponent<AngledEntryMovement>().enabled = true;
break;
}
}

Conclusion:
Congratulations! You have successfully implemented various movement behaviors for enemies in your Unity game. With side-to-side, circular, and angled entry movements, your game will now have more dynamic and challenging enemy behaviors, making it more engaging and enjoyable for players. Feel free to experiment with different parameters and combine movements to create even more diverse enemy behaviors!

--

--

No responses yet