private Vector3 ObstacleAvoidance()
{
Vector3 steeringForce = Vector3.zero;
// Cast a sphere, that bounds the avoidance zone of the agent, to detect obstacles
RaycastHit[] hits = Physics.SphereCastAll(this.transform.position, this.col.bounds.extents.x + this.avoidanceRadius, this.rb.velocity, this.forwardDetection);
// Compute and sum the forces across all hits
for(int i = 0; i < hits.Length; i++) {
// Ensure that the collidier is on a different object
if (hits[i].collider.gameObject.GetInstanceID () != this.gameObject.GetInstanceID ()) {
if (hits[i].distance > 0) {
// Scale the force inversely proportional to the distance to the target
float scaledForce = ((this.forwardDetection - hits[i].distance) / this.forwardDetection) * this.maxForce;
float desiredForce = Mathf.Min (scaledForce, this.maxForce);
// Compute the steering force
steeringForce += hits[i].normal * desiredForce;
}
}
}
return steeringForce;
}