0
0
Unityframework~7 mins

Enemy patrol and chase patterns in Unity

Choose your learning style9 modes available
Introduction

Enemy patrol and chase patterns make game characters move in smart ways. They help enemies look around and follow the player when close.

When you want enemies to walk back and forth on a path.
When enemies should follow the player after spotting them.
To make game levels more exciting with moving enemies.
When creating stealth or action games with enemy AI.
To add challenge by having enemies chase the player.
Syntax
Unity
using UnityEngine;

public class EnemyPatrolChase : MonoBehaviour
{
    public Transform[] patrolPoints;
    public float speed = 2f;
    public float chaseSpeed = 4f;
    public float chaseRange = 5f;
    private int currentPointIndex = 0;
    private Transform player;

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
    }

    void Update()
    {
        float distanceToPlayer = Vector3.Distance(transform.position, player.position);

        if (distanceToPlayer < chaseRange)
        {
            ChasePlayer();
        }
        else
        {
            Patrol();
        }
    }

    void Patrol()
    {
        Transform targetPoint = patrolPoints[currentPointIndex];
        transform.position = Vector3.MoveTowards(transform.position, targetPoint.position, speed * Time.deltaTime);

        if (Vector3.Distance(transform.position, targetPoint.position) < 0.1f)
        {
            currentPointIndex = (currentPointIndex + 1) % patrolPoints.Length;
        }
    }

    void ChasePlayer()
    {
        transform.position = Vector3.MoveTowards(transform.position, player.position, chaseSpeed * Time.deltaTime);
    }
}

Use Vector3.MoveTowards to smoothly move the enemy.

Check distance to player to switch between patrol and chase.

Examples
This moves the enemy between patrol points in order, looping back to the first point.
Unity
void Patrol()
{
    // Move between points in a loop
    Transform target = patrolPoints[currentPointIndex];
    transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    if (Vector3.Distance(transform.position, target.position) < 0.1f)
        currentPointIndex = (currentPointIndex + 1) % patrolPoints.Length;
}
This makes the enemy chase the player at a faster speed.
Unity
void ChasePlayer()
{
    // Move enemy towards player faster
    transform.position = Vector3.MoveTowards(transform.position, player.position, chaseSpeed * Time.deltaTime);
}
Sample Program

This script makes an enemy move between set points. When the player comes close, the enemy chases the player. Otherwise, it keeps patrolling.

Unity
using UnityEngine;

public class EnemyPatrolChase : MonoBehaviour
{
    public Transform[] patrolPoints;
    public float speed = 2f;
    public float chaseSpeed = 4f;
    public float chaseRange = 5f;
    private int currentPointIndex = 0;
    private Transform player;

    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
    }

    void Update()
    {
        float distanceToPlayer = Vector3.Distance(transform.position, player.position);

        if (distanceToPlayer < chaseRange)
        {
            ChasePlayer();
        }
        else
        {
            Patrol();
        }
    }

    void Patrol()
    {
        Transform targetPoint = patrolPoints[currentPointIndex];
        transform.position = Vector3.MoveTowards(transform.position, targetPoint.position, speed * Time.deltaTime);

        if (Vector3.Distance(transform.position, targetPoint.position) < 0.1f)
        {
            currentPointIndex = (currentPointIndex + 1) % patrolPoints.Length;
        }
    }

    void ChasePlayer()
    {
        transform.position = Vector3.MoveTowards(transform.position, player.position, chaseSpeed * Time.deltaTime);
    }
}
OutputSuccess
Important Notes

Make sure your player GameObject has the tag "Player" set.

Patrol points must be set in the Unity Editor by dragging transforms.

Adjust speeds and chase range to fit your game feel.

Summary

Enemy patrols between points using smooth movement.

Enemy chases player when close enough.

Switching between patrol and chase is based on distance.