/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Objects.ItemAssist
{
using Opsive.Shared.Events;
using Opsive.UltimateCharacterController.Items.Actions;
using UnityEngine;
///
/// The ShieldCollider component specifies the object that acts as a collider for the shield.
///
public class ShieldCollider : MonoBehaviour
{
[Tooltip("A reference to the Shield item action.")]
[SerializeField] protected Shield m_Shield;
[Tooltip("Is the collider attached to a Shield used for the first person perspective?")]
[HideInInspector] [SerializeField] protected bool m_FirstPersonPerspective;
[Shared.Utility.NonSerialized] public Shield Shield { get { return m_Shield; } set { m_Shield = value; } }
[Shared.Utility.NonSerialized] public bool FirstPersonPerspective { set { m_FirstPersonPerspective = value; } }
private Collider m_Collider;
private GameObject m_Character;
///
/// Initialize the default values.
///
private void Awake()
{
if (m_Shield == null) {
Debug.LogError("Error: The shield is not assigned. Ensure the shield is created from the Item Manager.", this);
return;
}
m_Collider = GetComponent();
m_Collider.enabled = false;
m_Character = m_Shield.gameObject.GetComponentInParent().gameObject;
EventHandler.RegisterEvent(m_Character, "OnCharacterChangePerspectives", OnChangePerspectives);
}
///
/// The camera perspective between first and third person has changed.
///
/// Is the camera in a first person view?
private void OnChangePerspectives(bool firstPersonPerspective)
{
// The collider should only be enabled for the corresponding perspective.
m_Collider.enabled = m_FirstPersonPerspective == firstPersonPerspective;
}
///
/// The object has been destroyed.
///
private void OnDestroy()
{
EventHandler.UnregisterEvent(m_Character, "OnCharacterChangePerspectives", OnChangePerspectives);
}
}
}