Menu
CoddyTech

Chase in Range

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

Most enemies leave you alone until you come close. Measure the distance to the player in every physics step, and chase only inside a range. This slime chases within sightRange and stands still outside it:

void FixedUpdate()
{
    Vector3 toPlayer = player.position - rb.position;
    toPlayer.y = 0f;
    if (toPlayer.magnitude < sightRange)
    {
        Vector3 dir = toPlayer.normalized;
        rb.linearVelocity = new Vector3(dir.x * speed, rb.linearVelocity.y, dir.z * speed);
    }
    else
    {
        rb.linearVelocity = new Vector3(0f, rb.linearVelocity.y, 0f);
    }
}

The else matters: a Rigidbody keeps its velocity until something changes it. Without it, a slime that loses you slides on until friction stops it, and on ice it never stops. Because y was set to 0 first, toPlayer.magnitude is the distance across the ground, so a player standing on a crate next to the slime still counts as next to it.

One range has a weak spot. A player standing on its edge drifts in and out of it, and the slime starts and stops over and over. Enemies avoid this with two ranges: they notice the player inside a short one and give up only beyond a longer one. A bool remembers whether the enemy is chasing, and between the two ranges nothing changes it, so the enemy carries on with what it was doing.

challenge icon

Challenge

Medium

A wolf hunts in the forest. Write the Wolf script on the Wolf. The Inspector gives it player, speed (3), noticeRange (4) and giveUpRange (8). The Player script is ready.

  • A bool field named chasing says whether the wolf is chasing. It starts false.
  • Every physics step, measure the distance to the player across the ground (no height). Closer than noticeRange: chasing becomes true. Farther than giveUpRange: it becomes false. In between, it stays as it is.
  • While chasing, the wolf runs at the player at speed with its Rigidbody, keeping its y velocity. Otherwise it stands still: x and z velocity 0.

Press Play, walk up to the wolf and run away, then Check.

Try it yourself

using UnityEngine;

public class Wolf : MonoBehaviour
{
    [SerializeField] private Transform player;
    [SerializeField] private float speed = 3f;
    [SerializeField] private float noticeRange = 4f;
    [SerializeField] private float giveUpRange = 8f;
    private Rigidbody rb;

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

    void FixedUpdate()
    {
        // notice the player, give up on it, and chase or stand still
    }
}
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