// Returns success if an object was found otherwise failure
public override TaskStatus OnUpdate()
{
GameObject found = null;
if (targetObjects.Value != null && targetObjects.Value.Count > 0) { // If there are objects in the group list then search for the object within that list
GameObject objectFound = null;
for (int i = 0; i < targetObjects.Value.Count; ++i) {
float audibility = 0;
GameObject obj;
if (Vector3.Distance(targetObjects.Value[i].transform.position, transform.position) < hearingRadius.Value) {
if ((obj = MovementUtility.WithinHearingRange(transform, offset.Value, audibilityThreshold.Value, targetObjects.Value[i], ref audibility)) != null) {
objectFound = obj;
}
}
}
found = objectFound;
}
else if (!string.IsNullOrEmpty(targetTag.Value))
{
var target = GameObject.FindGameObjectWithTag(targetTag.Value);
if (Vector3.Distance(target.transform.position, transform.position) < hearingRadius.Value)
{
found = MovementUtility.WithinHearingRange(transform, offset.Value, audibilityThreshold.Value, target);
}
}
else { // If the target object is null then determine if there are any objects within hearing range based on the layer mask
if (usePhysics2D) {
if (overlap2DColliders == null) {
overlap2DColliders = new Collider2D[maxCollisionCount];
}
found = MovementUtility.WithinHearingRange2D(transform, offset.Value, audibilityThreshold.Value, hearingRadius.Value, overlap2DColliders, objectLayerMask);
} else {
if (overlapColliders == null) {
overlapColliders = new Collider[maxCollisionCount];
}
found = MovementUtility.WithinHearingRange(transform, offset.Value, audibilityThreshold.Value, hearingRadius.Value, overlapColliders, objectLayerMask);
}
}
if (found != null) {
returnedObject.Value = found;
// returnedObject success if an object was heard
return TaskStatus.Success;
}
// An object is not within heard so return failure
return TaskStatus.Failure;
}