/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Demo
{
using Opsive.Shared.Game;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Game;
using Opsive.UltimateCharacterController.Utility;
using UnityEngine;
///
/// Adjusts the size of the trigger when the character enters. This is useful for movement type triggers so the character doesn't keep switching modes as the controls
/// change while on the edge of the trigger.
///
public class TriggerAdjustor : MonoBehaviour
{
[Tooltip("Specifies the amount to expand the BoxCollider trigger by.")]
[SerializeField] protected Vector3 m_BoxColliderExpansion;
[Tooltip("Specifies the amount to inflate the MeshCollider trigger by.")]
[SerializeField] protected Mesh m_ExpandedMesh;
private GameObject m_ActiveObject;
private Collider m_Collider;
private Mesh m_OriginalMesh;
private bool m_AllowTriggerExit = true;
///
/// Initialize the default values.
///
private void Awake()
{
m_Collider = GetComponent();
if (m_Collider is MeshCollider) {
m_OriginalMesh = (m_Collider as MeshCollider).sharedMesh;
}
}
///
/// An object has entered the trigger.
///
/// The object that entered the trigger.
private void OnTriggerEnter(Collider other)
{
if (m_ActiveObject != null || !MathUtility.InLayerMask(other.gameObject.layer, 1 << LayerManager.Character)) {
return;
}
var characterLocomotion = other.GetComponentInParent();
if (characterLocomotion == null) {
return;
}
// The object is a character. Expand the trigger.
if (m_Collider is BoxCollider) {
AdjustBoxCollider(m_Collider as BoxCollider, m_BoxColliderExpansion);
} else if (m_Collider is MeshCollider) {
// When the mesh is inflated it'll trigger an OnTriggerExit callback. Prevent this callback from doing anything until
// after the inflated mesh has stabalized.
m_AllowTriggerExit = false;
Scheduler.ScheduleFixed(Time.fixedDeltaTime * 2, () => { m_AllowTriggerExit = true; });
(m_Collider as MeshCollider).sharedMesh = m_ExpandedMesh;
}
m_ActiveObject = other.gameObject;
}
///
/// Adjusts the size of the BoxCollider.
///
/// The BoxCollider that should be adjusted.
/// The amount to adjust the BoxCollider by.
private void AdjustBoxCollider(BoxCollider boxCollider, Vector3 adjustment)
{
var size = boxCollider.size;
size += adjustment;
boxCollider.size = size;
}
///
/// An object has exited the trigger.
///
/// The collider that exited the trigger.
private void OnTriggerExit(Collider other)
{
if (!m_AllowTriggerExit) {
return;
}
if (m_ActiveObject == other.gameObject) {
if (m_Collider is BoxCollider) {
AdjustBoxCollider(m_Collider as BoxCollider, -m_BoxColliderExpansion);
} else if (m_Collider is MeshCollider) {
(m_Collider as MeshCollider).sharedMesh = m_OriginalMesh;
}
m_ActiveObject = null;
}
}
}
}