Performance: Obstacle avoidance
MEDIUM IMPACT
This affects the frame rate and responsiveness of the game by how often and how complex the obstacle detection and avoidance calculations are.
void Update() {
if (Time.frameCount % 5 == 0) {
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 10f)) {
if (hit.collider.CompareTag("Obstacle")) {
transform.Rotate(0, 90, 0);
}
}
}
transform.Translate(Vector3.forward * Time.deltaTime * speed);
}void Update() {
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 10f)) {
if (hit.collider.CompareTag("Obstacle")) {
transform.Rotate(0, 90, 0);
}
}
transform.Translate(Vector3.forward * Time.deltaTime * speed);
}| Pattern | Physics Calls | CPU Usage | Frame Drops | Verdict |
|---|---|---|---|---|
| Raycast every frame | High (1 per frame) | High | Frequent | [X] Bad |
| Raycast every 5 frames | Low (1 per 5 frames) | Low | Rare | [OK] Good |