0
0
Unityframework~5 mins

Waypoint systems in Unity

Choose your learning style9 modes available
Introduction

A waypoint system helps characters or objects move smoothly from one point to another in a game. It makes movement easy to control and predictable.

You want an enemy to patrol along a set path.
You want a character to follow a route automatically.
You want to create moving platforms that follow specific points.
You want to guide a player or NPC through checkpoints.
You want to animate objects moving between fixed locations.
Syntax
Unity
public class WaypointSystem : MonoBehaviour
{
    public Transform[] waypoints;
    private int currentWaypointIndex = 0;
    public float speed = 2f;

    void Update()
    {
        if (waypoints.Length == 0) return;

        Transform target = waypoints[currentWaypointIndex];
        Vector3 direction = target.position - transform.position;
        float step = speed * Time.deltaTime;

        if (direction.magnitude < step)
        {
            currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;
        }
        else
        {
            transform.position += direction.normalized * step;
        }
    }
}

Waypoints are usually stored as an array of Transform objects representing positions in the scene.

The object moves towards the current waypoint, then switches to the next when close enough.

Examples
Declare waypoints array, speed, and track current waypoint index.
Unity
public Transform[] waypoints;
public float speed = 3f;
private int currentWaypointIndex = 0;
Move the object towards the current waypoint each frame.
Unity
void Update()
{
    Transform target = waypoints[currentWaypointIndex];
    Vector3 direction = target.position - transform.position;
    transform.position += direction.normalized * speed * Time.deltaTime;
}
Check if close to waypoint, then move to the next one in a loop.
Unity
if (Vector3.Distance(transform.position, waypoints[currentWaypointIndex].position) < 0.1f)
{
    currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;
}
Sample Program

This script moves a game object smoothly between points set in the waypoints array. It moves towards the current target waypoint each frame. When it gets close enough, it switches to the next waypoint, looping back to the start.

Unity
using UnityEngine;

public class WaypointSystem : MonoBehaviour
{
    public Transform[] waypoints;
    private int currentWaypointIndex = 0;
    public float speed = 2f;

    void Update()
    {
        if (waypoints.Length == 0) return;

        Transform target = waypoints[currentWaypointIndex];
        Vector3 direction = target.position - transform.position;
        float step = speed * Time.deltaTime;

        if (direction.magnitude < step)
        {
            currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;
        }
        else
        {
            transform.position += direction.normalized * step;
        }
    }
}
OutputSuccess
Important Notes

Make sure waypoints are placed in the scene and assigned in the inspector.

Adjust speed to control how fast the object moves.

Use Vector3.Distance or magnitude to check proximity to waypoints.

Summary

Waypoint systems let objects move along set points automatically.

They use arrays of positions and move the object step-by-step each frame.

When close to a waypoint, the system switches to the next point in a loop.