/// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Samples { using Opsive.BehaviorDesigner.Runtime.Tasks; using Opsive.BehaviorDesigner.Runtime.Tasks.Conditionals; using Opsive.GraphDesigner.Runtime.Variables; using UnityEngine; /// /// Returns success as soon as the target object can be seen. /// This task is basic intended for demo purposes. For a more complete task see the Senses Pack: /// https://assetstore.unity.com/packages/slug/310244 /// [Shared.Utility.Category("Behavior Designer Samples")] public class CanSeeObject : Conditional { [Tooltip("The object that the agent is searching for.")] [SerializeField] protected SharedVariable m_Target; [Tooltip("The raycast offset to apply to the target.")] [SerializeField] protected SharedVariable m_TargetOffset; [Tooltip("The field of view angle of the agent (in degrees).")] [SerializeField] protected SharedVariable m_FieldOfView = 90; [Tooltip("The distance that the agent can see.")] [SerializeField] protected SharedVariable m_ViewDistance = 1000; /// /// Returns success if an object was found otherwise failure /// /// public override TaskStatus OnUpdate() { // Return success if an object was found. return WithinSight() ? TaskStatus.Success : TaskStatus.Failure; } /// /// Determines if the target object is within sight of the agent. /// /// True if the target is within sight. private bool WithinSight() { if (m_Target.Value == null) { return false; } var targetPosition = m_Target.Value.transform.TransformPoint(m_TargetOffset.Value); var direction = targetPosition - m_Transform.position; direction.y = 0; var angle = Vector3.Angle(direction, m_Transform.forward); if (direction.magnitude < m_ViewDistance.Value && angle < m_FieldOfView.Value * 0.5f) { RaycastHit hit; if (Physics.Linecast(m_Transform.position, targetPosition, out hit)) { if (hit.transform.IsChildOf(m_Target.Value.transform) || m_Target.Value.transform.IsChildOf(hit.transform)) { return true; } } } return false; } } }