/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Character
{
using Opsive.Shared.Events;
using UnityEngine;
///
/// Acts as a LookSource for any character that does not have a camera (or network) attached.
///
public class LocalLookSource : MonoBehaviour, ILookSource
{
[Tooltip("The distance that the character should look ahead.")]
[SerializeField] protected float m_LookDirectionDistance = 100;
[Tooltip("The location of the look source. The character's head is a good value.")]
[SerializeField] protected Transform m_LookTransform;
[Tooltip("The object that the character should look at.")]
[SerializeField] protected Transform m_Target;
public GameObject GameObject { get { return m_GameObject; } }
public Transform Transform { get { return m_Transform; } }
public float LookDirectionDistance { get { return m_LookDirectionDistance; } }
public float Pitch { get { return 0; } }
public Transform Target { get { return m_Target; } set { m_Target = value; } }
private GameObject m_GameObject;
private Transform m_Transform;
private bool m_Started;
///
/// Initialize the default values.
///
private void Awake()
{
m_GameObject = gameObject;
m_Transform = transform;
if (m_LookTransform == null) {
var animator = GetComponent();
if (animator != null) {
m_LookTransform = animator.GetBoneTransform(HumanBodyBones.Head);
}
if (m_LookTransform == null) {
m_LookTransform = m_Transform;
}
}
}
///
/// The component has been enabled.
///
private void OnEnable()
{
// Only attach the look source again if the component has already been started. By attaching the look source within enable it allows for the character to switch
// look sources between a player-controlled character and an AI character.
if (m_Started) {
EventHandler.ExecuteEvent(m_GameObject, "OnCharacterAttachLookSource", this);
}
}
///
/// The component has been started.
///
private void Start()
{
m_Started = true;
EventHandler.ExecuteEvent(m_GameObject, "OnCharacterAttachLookSource", this);
}
///
/// The component has been disabled.
///
private void OnDisable()
{
EventHandler.ExecuteEvent(m_GameObject, "OnCharacterAttachLookSource", null);
}
///
/// Returns the direction that the character is looking.
///
/// Is the character look direction being retrieved?
/// The direction that the character is looking.
public Vector3 LookDirection(bool characterLookDirection)
{
if (m_Target != null) {
return (m_Target.position - m_LookTransform.position).normalized;
}
return m_Transform.forward;
}
///
/// Returns the direction that the character is looking.
///
/// The position that the character is looking from.
/// Is the character look direction being retrieved?
/// The LayerMask value of the objects that the look direction can hit.
/// Should recoil be included in the look direction?
/// The direction that the character is looking.
public Vector3 LookDirection(Vector3 lookPosition, bool characterLookDirection, int layerMask, bool useRecoil)
{
if (m_Target != null) {
return (m_Target.position - lookPosition).normalized;
}
return m_Transform.forward;
}
///
/// Returns the position of the look source.
///
/// The position of the look source.
public Vector3 LookPosition()
{
return m_LookTransform.position;
}
}
}