Performance: Waypoint systems
This affects frame rate and responsiveness by controlling how often and how many path calculations and object movements occur per frame.
void Update() {
if (ReachedCurrentWaypoint()) {
currentWaypointIndex++;
}
MoveTowards(waypoints[currentWaypointIndex].position);
}void Update() {
foreach (var waypoint in waypoints) {
MoveTowards(waypoint.position);
}
}| Pattern | CPU Usage | Update Frequency | Frame Drops | Verdict |
|---|---|---|---|---|
| Checking all waypoints every frame | High | Every frame | Frequent | [X] Bad |
| Checking only current waypoint | Low | Only when needed | Rare | [OK] Good |
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]);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++;
}
}Pass the test and this pattern moves unknown