The Animal Simulation HerdSim framework relies on a modified variant of Craig Reynolds' classic Boids flocking algorithm, extended specifically for ground-based and aerial animal traversal within Unity environments. At its core, the system calculates steering vectors for individual entities by combining three primary behaviors—Separation, Alignment, and Cohesion—with specialized secondary behaviors such as terrain alignment, predator avoidance, and waypoint navigation.
Unlike simple Boids implementations that iterate over all dynamic transforms via direct Transform.position reads, HerdSim utilizes localized manager classes (e.g., HerdSimController) to cache positional data and reduce internal engine calls. The simulation loop processes movement in distinct phases:
cohesionWeight, separationWeight, obstacleWeight).To reduce garbage collection allocations, array resizing is constrained at runtime. Agent states are stored in flat, pre-allocated memory structures, minimizing heap allocations during high-count entity processing.
Developers looking to inject custom AI states (such as eating, fleeing, or resting) into the simulation loop can interface directly with the agent event system. The following C# code demonstrates how to override the default steering target when a custom environmental threat enters an agent's sensing zone:
using UnityEngine;
namespace HerdSim.Extensions
{
[RequireComponent(typeof(HerdSimAgent))]
public class ThreatAvoidanceOverride : MonoBehaviour
{
private HerdSimAgent _agent;
[SerializeField] private LayerMask _threatLayerMask;
[SerializeField] private float _detectionRadius = 15.0f;
[SerializeField] private float _fleeForceMultiplier = 2.5f;
private void Awake()
{
_agent = GetComponent<HerdSimAgent>();
}
private void Update()
{
EvaluateThreats();
}
private void EvaluateThreats()
{
Collider[] threats = Physics.OverlapSphere(transform.position, _detectionRadius, _threatLayerMask);
if (threats.Length > 0)
{
Vector3 averageThreatPos = Vector3.zero;
for (int i = 0; i < threats.Length; i++)
{
averageThreatPos += threats[i].transform.position;
}
averageThreatPos /= threats.Length;
Vector3 fleeDirection = (transform.position - averageThreatPos).normalized;
_agent.SetCustomSteeringVector(fleeDirection * _fleeForceMultiplier);
_agent.SetState(HerdAgentState.Fleeing);
}
}
}
}
The standard editor integration provides custom Inspector inspectors designed to simplify herd composition and behavior tuning without forcing developers to modify underlying codebase defaults. Parameters are logically segmented across global flock controllers and individual agent overrides.
When selecting a HerdSimController instance in the Unity Inspector, several critical properties dictate overall crowd density and stability:
Visual debugging features display realtime direction vectors and spatial boundaries inside the Scene View. When selected, agents draw gizmos indicating current velocity vectors, target vectors, ground raycast hit points, and neighbor detection perimeters. This allows for rapid iteration when troubleshooting flock bundling, erratic turning, or terrain clipping.
Simulating dozens to hundreds of concurrent animal models requires strict attention to target platforms, engine runtime settings, and physics configurations.
The primary CPU bottleneck in unoptimized flocking systems is $O(N^2)$ distance checking. HerdSim mitigates this through staggered update loops (allocating steering logic over multiple frames via frame-slice offsets) and internal grid queries.
Standard Unity rigidbodies with dynamic sphere colliders scale poorly for high agent counts due to continuous solver passes. HerdSim defaults to raycast-based surface probing. Running 100 concurrent agents using raycasts consumes a fraction of the frame time compared to dynamic dynamic body colliders.
Setting up a functioning animal simulation environment involves configuring manager prefabs, surface layer masks, and target waypoints.
Terrain) and dynamic obstacles (e.g., Obstacles).Terrain layer.HerdSimManager prefab from the project drawer into the scene hierarchy.Ground Layer Mask field to evaluate only the designated Terrain layer.Agent Prefab array slot.To dynamically instantiate and initialize a flock via C# script at runtime, use the following manager setup pattern:
using UnityEngine;
namespace HerdSim.Setup
{
public class HerdSpawner : MonoBehaviour
{
[SerializeField] private GameObject _herdControllerPrefab;
[SerializeField] private Vector3 _spawnCenter = Vector3.zero;
[SerializeField] private int _initialAgentCount = 50;
private void Start()
{
GameObject controllerObj = Instantiate(_herdControllerPrefab, _spawnCenter, Quaternion.identity);
HerdSimController controller = controllerObj.GetComponent<HerdSimController>();
if (controller != null)
{
controller.SetMaxAgents(_initialAgentCount);
controller.InitializeFlock();
}
}
}
}
| Category | Tools / Behavior AI |
|---|---|
| Latest Version | v1.4.3 |
| File Size | 29.33 MB |
| Added to Platform | February 5, 2026 |
| Views | 7 views |
| Downloads | 0 downloads |
| Favourites | 0 favourites |
| Issue with Package? | Report Broken Link → |