0
0
Unityframework~8 mins

Enemy patrol and chase patterns in Unity - Performance & Optimization

Choose your learning style9 modes available
Performance: Enemy patrol and chase patterns
MEDIUM IMPACT
This concept affects frame rate and responsiveness during gameplay by controlling how often enemy AI updates and moves.
Implementing enemy patrol and chase behavior in a game
Unity
void Update() {
  if (Vector3.Distance(transform.position, player.position) < chaseRange) {
    Vector3 direction = player.position - transform.position;
    transform.position += direction.normalized * speed * Time.deltaTime;
  } else {
    Patrol();
  }
}

void Patrol() {
  // Move between waypoints with less frequent updates
}
Only calculates chase movement when player is close, reducing unnecessary calculations.
📈 Performance GainReduces CPU usage by limiting chase logic to relevant frames, improving frame rate stability.
Implementing enemy patrol and chase behavior in a game
Unity
void Update() {
  Vector3 direction = player.position - transform.position;
  transform.position += direction.normalized * speed * Time.deltaTime;
  // No checks, runs every frame
}
Calculating chase direction and moving enemy every frame without conditions causes unnecessary CPU usage.
📉 Performance CostTriggers continuous CPU usage every frame, increasing frame time and risking frame drops.
Performance Comparison
PatternCPU UsageUpdate FrequencyFrame ImpactVerdict
Unconditional chase every frameHighEvery frameCauses frame drops[X] Bad
Conditional chase with distance checkLow to MediumOnly when player nearSmooth frame rate[OK] Good
Rendering Pipeline
Enemy patrol and chase logic runs on the CPU and affects the game loop update frequency, indirectly influencing frame rendering smoothness.
Game Logic Update
Physics Calculation
Frame Rendering
⚠️ BottleneckGame Logic Update stage due to frequent position calculations and AI decisions.
Optimization Tips
1Avoid running enemy AI calculations every frame without conditions.
2Use distance checks to limit chase logic to relevant situations.
3Optimize patrol paths and update frequency to reduce CPU load.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance issue with running enemy chase logic every frame without conditions?
AIt causes unnecessary CPU usage and can lower frame rate.
BIt improves AI responsiveness significantly.
CIt reduces memory usage.
DIt increases GPU load.
DevTools: Unity Profiler
How to check: Open Unity Profiler, run the game, and observe CPU usage and frame time during enemy patrol and chase.
What to look for: Look for spikes in CPU usage and frame time when enemies chase; consistent low CPU usage indicates good performance.