Implementing Steering Behaviors for Agent Navigation in Unity 3D
Steering Behaviors provide reactive navigation for autonomous agents in dynamic environments. In a Unity project, agents—three types of fish—move through an underwater maze, reacting to the player's submarine. Each type uses a combination of basic behaviors: seek, pursuit, evade, wander, and collision avoidance. This enables smooth trajectories without global pathfinding.
The approach relies on local vector calculations each frame. An agent adjusts its velocity and orientation toward a desired direction, considering targets, threats, and obstacles. Combining simple rules generates complex, natural movement.
Basic Types of Steering Behaviors
Steering Behaviors are divided into fundamental types, implemented via vector mathematics:
- Seek: calculating a vector from the agent's position to the target, normalizing it, and applying it as the desired velocity direction.
- Pursuit: predicting the target's position based on its speed and direction, then seeking the predicted point.
- Evade: pursuit in the opposite direction from the predicted threat position.
- Wander: a base direction with random deviations applied within a disk radius on a sphere ahead of the agent.
- Collision avoidance: scanning rays forward, steering away from the nearest obstacle.
These behaviors are summed with weights to produce the final steering force, limited by a maximum force.
In the underwater environment, collision avoidance is critical due to maze walls. Wander adds variability for edible fish outside the submarine's zone.
Behavior Models for Fish Agents
Fish are divided into edible, dangerous, and poisonous types. Each type corresponds to an algorithm based on Steering Behaviors:
- Following (for all types initially): seek toward the submarine with slight random noise to avoid linearity.
- Pursuit (dangerous/poisonous): pursuit with smooth direction smoothing.
- Avoidance (edible): transitioning from seek to evade + lateral offset upon entering the interaction zone.
Smoothness is ensured by interpolating current and desired directions with a coefficient α ∈ [0,1]. Speed is limited by v_max, and lifetime t_life ≤ T_max is considered.
Mathematical Implementation of Algorithms
Notations:
- x_s — submarine position
- x_f — fish position
- v_f — fish velocity
- Δt — timestep
- R — zone radius
- v_max — maximum speed
- u — up vector
- t_life — lifetime
- T_max — maximum lifetime
Distance: d = ||x_s - x_f||
Unit vector to target: e_t = (x_s - x_f) / ||x_s - x_f||
Following
If d > R
e_follow = e_t
With noise: e_follow = normalize(e_follow + k_r r), where r is a random vector ||r|| < ε
Pursuit
e_p = e_t
Smoothed: e_new = normalize((1-α) e_cur + α e_p)
v_f = v_max * e_new
Avoidance
If d > R: e = e_follow*
Else: e_evade = -e_t + lateral_offset (perpendicular to u × e_t)
v_f = v_max * normalize(e_evade)
Position update: x_f += v_f * Δt
These formulas are integrated into the agent's Update() in Unity. Collision avoidance is added as a priority steering force upon detecting raycast hits.
Integration into Unity and Optimization
In Unity, the implementation uses Transform for position/rotation, Rigidbody for physics (optional). Steering force is applied via AddForce or by directly setting velocity.
Key performance aspects:
- Local computations without navmesh.
- Limiting raycasts for avoidance (3-5 rays in a cone).
- Pooling fish for spawning/despawning.
Behavior adapts to the 3D maze: vertical movements account for the u-vector, obstacles—walls from Collider.
For senior developers: extend the model with arrival (slowing near the target), separation (distance between agents), cohesion/flocking for fish groups.
Key Takeaways
- Locality: Steering Behaviors avoid expensive A*-like planning, suitable for 50+ agents.
- Combinability: summing forces with weights allows layering behaviors (wander + seek + avoid).
- Reactivity: instant adaptation to submarine movement without replanning.
- Smoothness: interpolation prevents jitter, realistic for underwater.
- Extensibility: easy to add leader-following or group behaviors.
— Editorial Team
No comments yet.