/// --------------------------------------------- /// Ultimate Character Controller /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.UltimateCharacterController.Camera { using Opsive.Shared.Events; using Opsive.Shared.Game; using Opsive.UltimateCharacterController.Input; using Opsive.UltimateCharacterController.StateSystem; using UnityEngine; /// /// Handles the input for the AimAssist component. /// [RequireComponent(typeof(AimAssist))] public class AimAssistHandler : StateBehavior { [Tooltip("Can the targets be switched?")] [SerializeField] protected bool m_CanSwitchTargets; [Tooltip("The name of the button mapping for switching targets.")] [SerializeField] protected string m_SwitchTargetInputName = "Horizontal"; [Tooltip("The minimum magnitude required to switch targets.")] [SerializeField] protected float m_SwitchTargetMagnitude = 0.8f; public bool CanSwitchTargets { get { return m_CanSwitchTargets; } set { m_CanSwitchTargets = value; if (Application.isPlaying) { enabled = m_PlayerInput != null && m_CanSwitchTargets; m_AllowTargetSwitch = true; } } } public string SwitchTargetInputName { get { return m_SwitchTargetInputName; } set { m_SwitchTargetInputName = value; } } public float SwitchTargetMagnitude { get { return m_SwitchTargetMagnitude; } set { m_SwitchTargetMagnitude = value; } } private AimAssist m_AimAssist; private PlayerInput m_PlayerInput; private bool m_AllowTargetSwitch = true; /// /// Initialize the default values. /// protected override void Awake() { base.Awake(); m_AimAssist = gameObject.GetComponent(); EventHandler.RegisterEvent(gameObject, "OnCameraAttachCharacter", OnAttachCharacter); // Enable after the character has been attached. enabled = false; } /// /// Attaches the component to the specified character. /// /// The handler to attach the camera to. protected virtual void OnAttachCharacter(GameObject character) { m_PlayerInput = character != null ? character.GetCachedComponent() : null; enabled = m_PlayerInput != null && m_CanSwitchTargets; } /// /// Tries to switch targets if the input value is large enough. /// private void Update() { var value = m_PlayerInput.GetAxisRaw(m_SwitchTargetInputName); if (m_AllowTargetSwitch && Mathf.Abs(value) > m_SwitchTargetMagnitude) { m_AimAssist.TrySwitchTargets(value > 0); m_AllowTargetSwitch = false; } else if (!m_AllowTargetSwitch && Mathf.Abs(value) < 0.01f) { // Don't allow another target switch until the value is reset. This will prevent the target from quickly switching. m_AllowTargetSwitch = true; } } /// /// The camera has been destroyed. /// private void OnDestroy() { EventHandler.UnregisterEvent(gameObject, "OnCameraAttachCharacter", OnAttachCharacter); } } }