33 lines
1.1 KiB
C#
33 lines
1.1 KiB
C#
using UnityEngine;
|
|
|
|
public class CameraFollow : MonoBehaviour
|
|
{
|
|
[SerializeField] private Transform target;
|
|
[SerializeField] private Vector3 offset = new Vector3(0f, 18f, -10f);
|
|
[SerializeField] private Vector3 rotationEuler = new Vector3(65f, 0f, 0f);
|
|
[SerializeField] private float smoothSpeed = 8f;
|
|
[SerializeField] private float rotationSmoothSpeed = 10f;
|
|
|
|
private void Awake()
|
|
{
|
|
// Upgrade the old chase-camera setup to the new top-down view for existing scenes.
|
|
if (offset == new Vector3(0f, 6f, -8f))
|
|
{
|
|
offset = new Vector3(0f, 18f, -10f);
|
|
}
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (target == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Vector3 desiredPosition = target.position + offset;
|
|
transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
|
|
Quaternion desiredRotation = Quaternion.Euler(rotationEuler);
|
|
transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, rotationSmoothSpeed * Time.deltaTime);
|
|
}
|
|
}
|