Life Engine 3D is an advanced 3D simulation of virtual humans powered by a custom behavior tree AI system in Unity. Agents interact with a dynamic environment to satisfy metabolic needs, seek thermal comfort, gather resources, craft tools, and build structures.
graph TD
HB[HumanBrain] --> BT[Behavior Tree Engine]
HB --> HL[HumanLocomotion]
HB --> HP[HumanPerception]
HB --> HM[HumanMemory]
HP --> HM
BT --> HL
BT --> HP
BT --> HM
The HumanBrain.cs component acts as the central coordinator for simulated human agents. It manages metabolic variables, evaluates local environment factors, and updates agent states.
- Metabolic Drives:
- Adenosine (Sleep): Accumulates at a rate of
5.625nM per in-game hour while awake, and clears at11.25nM per hour while sleeping. When high, agents are driven to seek shelter and sleep. - Ghrelin (Hunger): Accumulates at
140pg/mL per hour, triggering food-seeking behaviors when passing the hunger threshold of1200.
- Adenosine (Sleep): Accumulates at a rate of
- Thermal Comfort & Hysteresis:
- Perceived temperature ranges between comfort bounds (default
18°Cto26°C). - Shade Detection: When in daylight, a 5-point raycast silhouette check (Head, Center, Left/Right Shoulders, Feet) is projected towards the sun direction. If all 5 points are blocked by walls or tall trees, the agent is considered in shade, dropping perceived temperature by
10°C. - Hysteresis Filtering: Ambient temperature changes are interpolated smoothly (
Mathf.MoveTowards) with a2°Cbuffer zone to prevent rapid, unstable state transitions.
- Perceived temperature ranges between comfort bounds (default
The HumanLocomotion.cs component features an industry-standard navigation system decoupled from Unity's default agent movement to handle simulations at high speed multipliers (up to 8x).
- Decoupled NavMeshAgent:
updatePositionandupdateRotationare disabled. TheNavMeshAgentis used purely as an invisible path calculator. - Corridor Flaring: Uses
NavMesh.FindClosestEdgeto push steering targets0.25maway from walls if agents get within0.45mof edges, preventing clipping. - Predictive Wall Swerve: Utilizes left/right bumper rays (angled at
35°out to0.65m) to detect obstacles and steer away dynamically. - Local Repulsion: Nearby agents are detected using
Physics.OverlapSphereNonAllocand exert horizontal repulsion forces to prevent overlapping. - Glide Stabilization: Blends velocity profiles (30% new, 70% old) acting as a low-pass filter to eliminate high-frequency physics jitter.
- Hard Position Clamping: Clamps the Rigidbody position to the nearest NavMesh position via
NavMesh.SamplePositionif drift occurs. - Progress-Based Stuck Detection & Rescue Nudges: Checks progress every
0.4s. If distance to target hasn't decreased by0.05mand physical velocity is< 0.2m/s, it initiates a Rescue Nudge (micro-teleport perpendicular to current direction + path recalculation) to break solver locks.
Simulated humans scan and remember their environment using specialized sensory components.
- Perception (
HumanPerception.cs):- Sights & Angle: Scans a radius of
15macross a200°Field of View. - LOS Obstruction Raycasting: Performs raycasts from eye-level (
1.5m) using dynamic height offsets depending on target types (e.g.0.15mfor low items,0.6mfor tree trunks,0.8mfor other humans) to prevent incorrect visual blocks. - Hearing Radius: Objects within
2mare detected in a 360-degree circle regardless of line-of-sight. - Ground/Source Scans: Prioritizes picking up resources already lying on the ground before attempting to fell new trees or bushes.
- Sights & Angle: Scans a radius of
- Memory (
HumanMemory.cs):- Remembers threat positions for a duration (default
4.0s). - Position Merging: Automatically merges threats within
1.0mof each other to consolidate pathways and prevent redundant memory stacks.
- Remembers threat positions for a duration (default
The simulated world contains physical resources mapped in the ResourceRegistry.cs scriptable object.
- Resources (
ResourceType.cs):- Logs (
Log_1toLog_4representing weights). - Sticks (
Stick_1toStick_4representing lengths). - Stones and Sharpened Stones.
- Logs (
- Recipe Tree Fallbacks:
- If a specific resource length/weight is needed (e.g.,
Stick_2orLog_2), agents automatically evaluate the recipe tree. - Multi-Output Conversion: Agents can collect larger resources (e.g.,
Stick_3orLog_3) and split them, yielding the needed item alongside leftover smaller pieces.
- If a specific resource length/weight is needed (e.g.,
The agent evaluates behaviors starting from Priority 0 (Highest) down to Priority 6 (Lowest):
- Sleep Sequence: Evaluates adenosine level; navigates to shelter or sleeps in-place.
- Flee Sequence: Scans for danger in memory/perception; runs away.
- Eat Sequence: Triggers when hungry; scans for food and consumes it.
- Seek Shelter Sequence: Searches for shelter when bad weather or night falls.
- Thermal Comfort:
- Seek Shade if overheating.
- Seek Warmth if cold (heats from fires, or initiates campfire crafting).
- Fell Tree (Test Mode): Checks for axes; picks up axes or initiates axe crafting, then chops trees.
- Wander Sequence: Wanders within local bounds.
- Unity Editor (compatible with 3D URP pipelines).
- Open the project in Unity.
- Open the main scene:
Assets/Scenes/SampleScene.unity. - Press the Play button in the Editor.
- Use the mouse to select agents in the world to view their active behavior tree state and metabolic levels. Use the UI panel to adjust simulation time-scale.