Menu
CoddyTech

Patrol and Chase

Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 52 of 63.

A guard on patrol walks a route until it sees the player, chases, and goes back to its route once the player is gone. Both behaviors steer the same way, toward a point, so one method can do the walking for both. This sentry walks back and forth between two posts and chases the player within chaseRange:

[SerializeField] private Transform pointA;
[SerializeField] private Transform pointB;
[SerializeField] private Transform player;
[SerializeField] private float speed = 2f;
[SerializeField] private float chaseRange = 5f;
private Rigidbody rb;
private Transform goal;

void Start()
{
    rb = GetComponent<Rigidbody>();
    goal = pointA;
}

void FixedUpdate()
{
    if (Flat(player.position - rb.position).magnitude < chaseRange)
    {
        WalkTo(player.position);
        return;
    }
    if (Flat(goal.position - rb.position).magnitude < 0.2f)
    {
        goal = goal == pointA ? pointB : pointA;
    }
    WalkTo(goal.position);
}

void WalkTo(Vector3 target)
{
    Vector3 dir = Flat(target - rb.position).normalized;
    rb.linearVelocity = new Vector3(dir.x * speed, rb.linearVelocity.y, dir.z * speed);
}

Vector3 Flat(Vector3 v)
{
    return new Vector3(v.x, 0f, v.z);
}

The chase test comes first, and its return skips the patrol code, so chasing wins whenever the player is close. goal is a field, so the sentry remembers it through a chase: once the player is gone, it walks on to the post it was heading for.

The arrival test measures across the ground. The posts are markers on the floor and the sentry's center is 1 unit above them, so a distance with the height in it never drops below 1 and the sentry would never turn around. The test asks for less than 0.2, not exactly 0: at 2 units per second the sentry moves 0.04 units per step and almost never lands exactly on the post.

challenge icon

Challenge

Medium

A security robot patrols the warehouse. Write the Robot script. The Inspector gives it waypoints (the four yellow markers W0 to W3, in order), player, speed (4) and chaseRange (4). The Player script is ready.

  • An int field named next, starting at 0, is the index of the waypoint the robot is walking to.
  • Patrol: walk to waypoints[next]. Once the robot is closer than 0.2 to it across the ground, move next on to the following waypoint, and after the last one back to 0.
  • Chase: while the player is closer than chaseRange across the ground, walk to the player instead.
  • When the player gets away, carry on to waypoints[next], the waypoint it was heading for.
  • Move by setting the Rigidbody's velocity at speed across the ground, keeping its y velocity.

Press Play, watch a round of the patrol and cross its path, then Check.

Try it yourself

using UnityEngine;

public class Robot : MonoBehaviour
{
    [SerializeField] private Transform[] waypoints;
    [SerializeField] private Transform player;
    [SerializeField] private float speed = 4f;
    [SerializeField] private float chaseRange = 4f;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        // chase the player when it is close, or walk the patrol route
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Physics and Gameplay