/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Demo.AI
{
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Game;
using Opsive.UltimateCharacterController.Utility;
using UnityEngine;
///
/// Enables the attack on the agent when the character enters the trigger.
///
public class AttackTrigger : MonoBehaviour
{
[Tooltip("The agent doing the attacking.")]
[SerializeField] protected MeleeAgent m_Agent;
///
/// An object has exited the trigger.
///
/// The collider that exited the trigger.
private void OnTriggerEnter(Collider other)
{
// A main character collider is required.
if (!MathUtility.InLayerMask(other.gameObject.layer, 1 << LayerManager.Character)) {
return;
}
// The object must be a character.
var characterLocomotion = other.GetComponentInParent();
if (characterLocomotion == null) {
return;
}
m_Agent.Attack();
}
///
/// An object has entered the trigger.
///
/// The object that entered the trigger.
private void OnTriggerExit(Collider other)
{
// A main character collider is required.
if (!MathUtility.InLayerMask(other.gameObject.layer, 1 << LayerManager.Character)) {
return;
}
// The object must be a character.
var characterLocomotion = other.GetComponentInParent();
if (characterLocomotion == null) {
return;
}
m_Agent.CancelAttack();
}
}
}