Consider a Unity C# script that moves an object through waypoints in a loop. What will be the value of currentWaypointIndex after running the Update method once if currentWaypointIndex starts at 2 and there are 4 waypoints?
int currentWaypointIndex = 2; int totalWaypoints = 4; void Update() { currentWaypointIndex = (currentWaypointIndex + 1) % totalWaypoints; }
Think about how the modulo operator (%) works with the total number of waypoints.
The code increments the index by 1 and then uses modulo to wrap around. Starting at 2, adding 1 gives 3, and 3 % 4 is 3, so the index becomes 3.
Choose the best description of what a waypoint system does in a Unity game.
Think about how characters or objects move along predefined paths.
A waypoint system is used to define points in space that an object can move between, creating a path or patrol route.
Examine the code below. The object moves to the first waypoint but then stops. What is the cause?
public Transform[] waypoints; private int currentWaypoint = 0; public float speed = 5f; void Update() { transform.position = Vector3.MoveTowards(transform.position, waypoints[currentWaypoint].position, speed * Time.deltaTime); if (transform.position == waypoints[currentWaypoint].position) { currentWaypoint++; } }
Think about how floating point numbers behave when compared for equality.
Using '==' to compare Vector3 positions can fail because floating point values may never be exactly equal. This causes the condition to fail and the waypoint index not to update.
Choose the correct syntax to declare a public list of waypoints as Transforms in Unity C#.
Remember how to instantiate generic lists in C#.
Option A correctly declares and initializes a generic List of Transform objects. Other options have syntax errors or missing type parameters.
Given the code below, what will be the value of currentWaypoint after calling NextWaypoint() three times starting from 0, if there are 3 waypoints?
int currentWaypoint = 0; int totalWaypoints = 3; void NextWaypoint() { currentWaypoint = (currentWaypoint + 1) % totalWaypoints; }
Calculate the index step by step using modulo arithmetic.
Starting at 0, after first call: (0+1)%3 = 1; second call: (1+1)%3 = 2; third call: (2+1)%3 = 0. So after three calls, currentWaypoint is 0.