Class ObstacleAvoidanceBehavior
Steering behaviour that deflects the actor's heading away from detected obstacles using a forward sphere-cast fan.
public class ObstacleAvoidanceBehavior : SteeringBehaviour, IHandler
Inheritance
Implements
Inherited Members
- SteeringBehaviour.m_Solver
- SteeringBehaviour.Handle(object)
- SteeringBehaviour.SetNext(IHandler)
- SteeringBehaviour.Start()
- SteeringBehaviour.GetSteering(Vector3)
Remarks
A sphere-cast fan (multiple rays spread across ray_Angle degrees) is used instead
of a single forward ray because a single ray misses obstacles that are slightly off-axis
— common in narrow street scenarios. The sphere radius adds lateral margin so that the
actor does not clip obstacles that are only just inside the physical collider boundary.
ObstacleToIgnore allows the actor's own collider (and collaborating actors such
as the player) to be excluded from avoidance, preventing self-avoidance loops where the
actor would try to steer away from itself.
Fields
ObstacleToIgnore
The list of obstacles that should be ignored during obstacle avoidance. These are objects that the behavior will not consider as obstacles.
[SerializeField]
protected GameObject[] ObstacleToIgnore
Field Value
ray_Angle
The angle spread for the rays to be cast. This defines how wide the spread of the rays will be, measured in degrees.
[Range(0, 360)]
public int ray_Angle
Field Value
int
ray_numer
The number of rays to cast in different directions for obstacle detection. This allows for multi-ray casting for better detection coverage.
[Min(0)]
public int ray_numer
Field Value
int
Methods
GetSteering(Vector3)
Calculates the steering force to avoid obstacles while moving towards the target.
public override Vector3 GetSteering(Vector3 TargetVector)
Parameters
| Name | Type | Description |
|---|---|---|
TargetVector | Vector3 | The accumulated steering vector passed in from earlier chain nodes. |
Returns
Vector3 — The steering force to avoid obstacles while moving towards the target.
Remarks
The early-exit guard if (Velocity == Vector3.zero) return TargetVector is a
safety check, not a performance optimisation. Vector3)
called with zero as its forward argument throws an
ArgumentException in Unity because a zero-length vector has no
defined orientation. Returning the unchanged TargetVector when the
actor is stationary is correct behaviour as well: there is nothing to deflect away from
a direction that does not yet exist.
IsIgnored(RaycastHit)
Checks if the given raycast hit should be ignored (i.e., it hits an object to be avoided).
protected bool IsIgnored(RaycastHit hit)
Parameters
| Name | Type | Description |
|---|---|---|
hit | RaycastHit | The raycast hit result. |
Returns
bool — True if the hit should be ignored, false otherwise.