Files
2026-02-21 16:45:37 +08:00

67 lines
2.1 KiB
C#

using UnityEngine;
namespace BehaviorDesigner.Runtime.Tasks.Movement
{
[HelpURLAttribute("http://www.opsive.com/assets/BehaviorDesigner/Movement/documentation.php?id=12")]
[TaskIcon("Assets/Behavior Designer Movement/Editor/Icons/{SkinColor}CanHearObjectIcon.png")]
[TaskCategory("Movement")]
[TaskDescription("Check to see if the any objects are within hearing range of the current agent.")]
public class CanHearObject : Conditional
{
[Tooltip("Should the 2D version be used?")]
public bool usePhysics2D;
[Tooltip("The object that we are searching for. If this value is null then the objectLayerMask will be used")]
public SharedGameObject targetObject;
[Tooltip("The LayerMask of the objects that we are searching for")]
public LayerMask objectLayerMask;
[Tooltip("How far away the unit can hear")]
public SharedFloat hearingRadius = 50f;
[Tooltip("The further away a sound source is the less likely the agent will be able to hear it. Set a threshold for the the minimum audibility level that the agent can hear")]
public SharedFloat audibilityThreshold = 0.05f;
[Tooltip("The offset relative to the pivot position")]
public SharedVector3 offset;
[Tooltip("The returned object that is heard")]
public SharedGameObject returnedObject;
public override TaskStatus OnUpdate()
{
if (targetObject.Value == null)
{
if (usePhysics2D)
{
returnedObject.Value = MovementUtility.WithinHearingRange2D(transform, offset.Value, audibilityThreshold.Value, hearingRadius.Value, objectLayerMask);
}
else
{
returnedObject.Value = MovementUtility.WithinHearingRange(transform, offset.Value, audibilityThreshold.Value, hearingRadius.Value, objectLayerMask);
}
}
else
{
returnedObject.Value = MovementUtility.WithinHearingRange(transform, offset.Value, audibilityThreshold.Value, targetObject.Value);
}
if (returnedObject.Value != null)
{
return TaskStatus.Success;
}
return TaskStatus.Failure;
}
public override void OnReset()
{
hearingRadius = 50f;
audibilityThreshold = 0.05f;
}
public override void OnDrawGizmos()
{
}
}
}