/// ---------------------------------------------
/// 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.
///
[Shared.Utility.Category("Behavior Designer Samples")]
public class HasTakenDamage : Conditional
{
[Tooltip("The object that the agent is searching for.")]
[SerializeField] protected SharedVariable m_Target;
[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 direction = m_Target.Value.transform.position - 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, m_Target.Value.transform.position, out hit)) {
if (hit.transform.IsChildOf(m_Target.Value.transform) || m_Target.Value.transform.IsChildOf(hit.transform)) {
return true;
}
}
}
return false;
}
}
}