Skip to main content

Natural Crowd AI

NPCs perform autonomous, natural crowd behaviors using NavMeshAgent.

Overview

Typical NPC:
Move to target point

NPC Mentality:
- Avoid each other
- Browse shops
- Look at nearby people
- Stop suddenly
- Detour around obstacles

Component

CrowdNPC — MonoBehaviour. Requires a NavMeshAgent on the same GameObject.

FieldTypeDefaultDescription
WanderRadiusfloat10fWander radius (meters)
BehaviorChangeCooldownfloat3fBehavior switch interval (seconds)
CurrentBehaviorCrowdBehaviorTypeCurrent behavior (read-only)

CrowdBehaviorType Enum

public enum CrowdBehaviorType
{
Wander, // Random movement within radius
BrowseShop, // Slowly move toward nearby shop
LookAround, // Stop and look around (1-3s)
StopAndObserve, // Complete stop (1-2s)
AvoidObstacle, // Detour around obstacle
Hurt, // Stumble slightly as if bumped
Idle // Stay in place
}

Behavior Details

BehaviorSpeedDescription
WanderNormalMove to random destination within radius
BrowseShop×0.5 (slow)Move near object tagged "Shop"
LookAround0 (stop)Rotate slowly for 1~3 seconds
StopAndObserve0 (stop)Complete stop for 1~2 seconds
AvoidObstacleNormalMove to slightly offset new destination
Idle0 (stop)Do nothing

Example

Basic Usage

// 1. Add NavMeshAgent to GameObject
// 2. Add CrowdNPC component
// 3. Configure in Inspector, then press Play — works automatically

Shop Setup

For BrowseShop to work, add the Shop tag to shop objects.

// In Editor: Shop GameObject → Tag → "Shop"

Tips

  • Lower BehaviorChangeCooldown for more erratic behavior, higher for more purposeful.
  • Connect with WorldTimeSystem to increase Idle frequency at night.
  • Combine with EmotionController to change movement speed based on emotion.