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.
Practice
Solution
Step 1: Understand waypoint system concept
A waypoint system defines points in space for an object to move between.Step 2: Identify the main function
The main function is to move objects smoothly from one point to another.Final Answer:
To move an object smoothly between predefined points -> Option BQuick Check:
Waypoint system = smooth movement between points [OK]
- Confusing waypoints with collision detection
- Thinking waypoints change object appearance
- Assuming waypoints handle sound
Solution
Step 1: Recall C# array syntax
In C#, arrays are declared with the type followed by [] before the variable name.Step 2: Check each option
public Vector3[] waypoints; uses 'public Vector3[] waypoints;' which is correct syntax.Final Answer:
public Vector3[] waypoints; -> Option AQuick Check:
Array syntax = type[] name [OK]
- Placing [] after variable name incorrectly
- Missing 'new' keyword when initializing
- Using invalid assignment syntax
Vector3[] waypoints = {new Vector3(0,0,0), new Vector3(1,0,0), new Vector3(2,0,0)};
int currentWaypoint = 0;
currentWaypoint++;
Debug.Log(waypoints[currentWaypoint]);Solution
Step 1: Understand array and index
The array has 3 points at indices 0, 1, and 2.Step 2: Track currentWaypoint value
currentWaypoint starts at 0, then increments to 1 before logging.Step 3: Identify logged value
waypoints[1] is Vector3(1,0,0), so output is (1.0, 0.0, 0.0).Final Answer:
(1.0, 0.0, 0.0) -> Option CQuick Check:
Index 1 in array = (1,0,0) [OK]
- Forgetting index increment
- Confusing array indices starting at 1
- Expecting an error due to increment
public Vector3[] waypoints;
private int currentWaypoint = 0;
void Update() {
transform.position = Vector3.MoveTowards(transform.position, waypoints[currentWaypoint], 5 * Time.deltaTime);
if(transform.position == waypoints[currentWaypoint]) {
currentWaypoint++;
}
}Solution
Step 1: Check Vector3 comparison
Using '==' to compare Vector3 can fail due to floating point precision.Step 2: Identify correct comparison method
Use Vector3.Distance or check if distance is very small instead.Final Answer:
Using '==' to compare Vector3 positions can cause errors -> Option AQuick Check:
Vector3 equality check needs tolerance, not '==' [OK]
- Assuming '==' works perfectly for Vector3
- Ignoring array initialization issues
- Changing waypoint index direction incorrectly
Solution
Step 1: Understand looping with modulo
Using modulo (%) with array length loops index back to 0 after last index.Step 2: Analyze each option
currentWaypoint = (currentWaypoint + 1) % waypoints.Length; correctly uses modulo to loop index safely within bounds.Final Answer:
currentWaypoint = (currentWaypoint + 1) % waypoints.Length; -> Option DQuick Check:
Modulo operator loops index correctly [OK]
- Using > instead of >= for index check
- Not resetting index properly causing errors
- Decrementing index instead of incrementing
Ready to prove it?
Pass the test and this pattern moves unknown
