unity enemy follow player ai simple

Simple Enemy AI in Unity: How to Make Enemies Chase the Player Without Complex Pathfinding

Small Game Mechanics & Prototyping

You want a lightweight chase that works on mobile without building pathfinding by hand. The goal is to use Unity’s NavMeshAgent so the enemy can route around walls while keeping CPU and battery use low.

Drop-in C# script (find the Player by tag once, cache it, update the agent):

using UnityEngine;
using UnityEngine.AI;

public class Chase : MonoBehaviour
{
    NavMeshAgent agent;
    Transform target;

    void Awake()
    {
        agent = GetComponent();
        var go = GameObject.FindGameObjectWithTag("Player");
        if (go) target = go.transform;
    }

    void Update()
    {
        if (target != null) agent.SetDestination(target.position);
    }
}

Use the official NavMeshAgent docs for setup and baking guidance to avoid runtime surprises: see Unity Manual on NavMeshAgent. On mobile, tune agent.speed and obstacle avoidance to save battery and memory.

Common beginner mistake: using transform.LookAt plus manual forward movement and distance checks. That works in open areas but fails when geometry exists. Cache references and avoid frequent Find calls to reduce GC and CPU spikes.

Drop-in NavMeshAgent chase script that works

This drop-in agent script gets a character moving toward your target with minimal setup. The pattern replaces manual transform moves and distance checks with NavMeshAgent’s SetDestination and stoppingDistance.

Minimal C# script

Two valid variants exist: (1) a true minimal Update that calls SetDestination every frame for a single unit, and (2) a throttled repath that updates less often for many units on mobile. Cache the target’s transform once in Awake or Start to avoid repeated tag lookups and stutter seen in forum topics and later post follow-ups.

Inspector setup

  • Speed: 2–4
  • Acceleration: 8–16
  • Angular Speed: default or tuned for turn feel
  • stoppingDistance: 1.25–2.0 (slightly larger on mobile)

Quick verification

30-second test: hit Play, enable Scene path view, confirm the agent draws a route, moves toward the player, and stops at stoppingDistance without overlapping the capsule.

Beginner pitfall checklist: don’t drive motion with Rigidbody velocity at the same time. Use kinematic Rigidbody if you need physics hits. Follow Unity docs for NavMeshAgent parameters, cache references, and avoid allocations in Update to save debugging time.

Scene setup: make the enemy able to walk on a baked NavMesh

Before agents move, the scene must have a baked NavMesh that matches your level geometry. A correct bake stops a lot of “it didn’t work” posts and saves debugging time.

Mark surfaces and bake navigation

Mark ground and level meshes as Navigation Static. Open the Navigation or AI Navigation window, set walkable areas, then hit Bake so the editor produces the blue NavMesh surface.

Quick validation: you should see the blue surface in Scene view. Your spawn point must sit on that blue area—being a few centimeters off can prevent movement.

Agent radius, height, and tight mobile levels

Small doorways and tight corners are common on phone-sized maps. Match Agent Radius and Height to your character capsule and level metrics.

  • If an agent won’t pass a door, reduce radius and rebake.
  • Or widen door geometry slightly for clarity on small screens.

Common gotchas and mobile performance tips

Typical issues: missing NavMesh, mesh baked for a different agent type, or the agent starts off-mesh. Don’t crank speed to “force” passage—fix the clearance instead.

Bake at edit time; avoid runtime NavMesh changes on low-end devices. For debugging, slow the agent and increase stoppingDistance to confirm pathing, then tune back for feel.

Community topics and older post answers often point back to scene setup rather than scripting. For official guidance, consult the Unity Navigation/NavMesh docs in the manual.

unity enemy follow player ai simple with range, stop distance, and “chase only when close”

.

Only start pathing when the target is nearby, and stop when you reach a safe distance.

Implement a chaseRange and use agent.stoppingDistance for the stop value. Compute squared distance to avoid Vector3.Distance cost, and run SetDestination on a short repath timer only while inside range.

Prevent jitter by respecting agent.stoppingDistance: stop calling SetDestination when within that radius or set the destination to the agent’s current position. Don’t rotate or move the transform manually while the agent controls motion.

Two options work well: a distance check (cheap for many units) or a SphereCollider trigger (easy to author but watch trigger overhead on low-end phones).

Common mistakes in later post topics: missing tags, chaseRange smaller than stoppingDistance, wrong world-unit scale, or forgetting a baked NavMesh. Quick tuning: set chaseRange by encounter size, clamp active enemies, and cap repath frequency to save battery.

Leave a Reply

Your email address will not be published. Required fields are marked *