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.
Waypoint systems in 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.
public Transform[] waypoints; public float speed = 3f; private int currentWaypointIndex = 0;
void Update()
{
Transform target = waypoints[currentWaypointIndex];
Vector3 direction = target.position - transform.position;
transform.position += direction.normalized * speed * Time.deltaTime;
}if (Vector3.Distance(transform.position, waypoints[currentWaypointIndex].position) < 0.1f) { currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length; }
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.
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; } } }
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.
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.