Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.0 kB
1
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using Unity.VisualScripting;
using UnityEngine;

public class CameraScript : MonoBehaviour
{
    Transform target;
	[SerializeField] float smoothSpeed = 0.125f;
    [SerializeField]public Vector3 locationOffset;
    [SerializeField]public Vector3 rotationOffset;

	public static CameraScript Instance { get; private set; }

	void Awake() {
		Instance = this;
	}
	public void SetCameraTarget(Transform target) {
		this.target = target;
	}

	public void CameraUpdate() {
		if(!target) return;
		
		Vector3 desiredPosition = target.position + target.rotation * locationOffset;
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
        transform.position = smoothedPosition;

        Quaternion desiredrotation = target.rotation * Quaternion.Euler(rotationOffset);
        Quaternion smoothedrotation = Quaternion.Lerp(transform.rotation, desiredrotation, smoothSpeed);
        transform.rotation = smoothedrotation;
	}
}
Leave a Comment