/// ---------------------------------------------
/// Behavior Designer
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.BehaviorDesigner.Samples
{
using UnityEngine;
///
/// Pans to camera to always have the target in view.
///
public class CameraPanner : MonoBehaviour
{
[Tooltip("The object that the camera should follow.")]
[SerializeField] protected Transform m_Target;
[Tooltip("The speed that the camera should be panned.")]
[SerializeField] protected float m_Speed = 5;
private Transform m_Transform;
private Quaternion m_StartTargetRotation;
private Vector3 m_Offset;
///
/// Initialize the default values.
///
private void Awake()
{
m_Transform = transform;
}
///
/// Sets the camera offset.
///
private void OnEnable()
{
m_StartTargetRotation = m_Target.rotation;
m_Offset = m_Target.InverseTransformPoint(m_Transform.position);
}
///
/// Pans the camera.
///
private void LateUpdate()
{
m_Transform.position = Vector3.MoveTowards(m_Transform.position, TransformPoint(m_Target.position, m_StartTargetRotation, m_Offset), m_Speed * Time.deltaTime);
}
///
/// Transforms the position from local space to world space. This is similar to Transform.TransformPoint but does not require a Transform.
///
/// The world position of the object.
/// The world rotation of the object.
/// The local position of the object.
/// The world space position.
private static Vector3 TransformPoint(Vector3 worldPosition, Quaternion rotation, Vector3 localPosition)
{
return worldPosition + (rotation * localPosition);
}
}
}