Movement/ColliderCheck/TwoDimensionalBoxChildColli...

44 lines
1.3 KiB
C#

using UnityEngine;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement.ColliderTrigger
{
public class TwoDimensionalBoxChildColliderTrigger : MonoBehaviour, IColliderTrigger
{
[SerializeField] private LayerMask colliderMask = 0;
private bool _isTrigerred = false;
private Collider2D[] results = new Collider2D[8];
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
public UnityEvent<bool> OnTriggered { get; protected set; } = null;
public bool IsTrigerred
{
get => _isTrigerred;
protected set
{
bool isNewValue = _isTrigerred != value;
_isTrigerred = value;
if (isNewValue)
OnTriggered.Invoke(value);
}
}
protected void Awake()
=> OnTriggered = new UnityEvent<bool>();
protected void FixedUpdate()
=> IsTrigerred = Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, 0, results, ColliderMask) > 0;
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
Gizmos.color = IsTrigerred ? Color.green : Color.red;
Gizmos.DrawWireCube(transform.position, transform.lossyScale);
}
#endif
}
}