using System;
using UnityEngine;
using UnityEngine.Serialization;
namespace Unity.Cinemachine
{
///
/// This is a CinemachineComponent in the Aim section of the component pipeline.
/// Its job is to place the camera on the Follow Target.
///
[AddComponentMenu("Cinemachine/Procedural/Position Control/Cinemachine Hard Lock to Target")]
[SaveDuringPlay]
[DisallowMultipleComponent]
[CameraPipeline(CinemachineCore.Stage.Body)]
[RequiredTarget(RequiredTargetAttribute.RequiredTargets.Tracking)]
[HelpURL(Documentation.BaseURL + "manual/CinemachineHardLockToTarget.html")]
public class CinemachineHardLockToTarget : CinemachineComponentBase
{
///
/// How much time it takes for the position to catch up to the target's position
///
[Tooltip("How much time it takes for the position to catch up to the target's position")]
[FormerlySerializedAs("m_Damping")]
public float Damping = 0;
Vector3 m_PreviousTargetPosition;
/// True if component is enabled and has a LookAt defined
public override bool IsValid { get => enabled && FollowTarget != null; }
/// Get the Cinemachine Pipeline stage that this component implements.
/// Always returns the Aim stage
public override CinemachineCore.Stage Stage { get => CinemachineCore.Stage.Body; }
///
/// Report maximum damping time needed for this component.
///
/// Highest damping setting in this component
public override float GetMaxDampTime() => Damping;
/// Applies the composer rules and orients the camera accordingly
/// The current camera state
/// Used for calculating damping. If less than
/// zero, then target will snap to the center of the dead zone.
public override void MutateCameraState(ref CameraState curState, float deltaTime)
{
if (!IsValid)
return;
Vector3 dampedPos = FollowTargetPosition;
if (VirtualCamera.PreviousStateIsValid && deltaTime >= 0)
dampedPos = m_PreviousTargetPosition + VirtualCamera.DetachedFollowTargetDamp(
dampedPos - m_PreviousTargetPosition, Damping, deltaTime);
m_PreviousTargetPosition = dampedPos;
curState.RawPosition = dampedPos;
}
}
}