You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Driftology/Assets/Scripts/CameraController.cs

88 lines
2.6 KiB
C#

2 months ago
using UnityEngine;
2 months ago
[RequireComponent(typeof(Camera))]
public class CameraController : MonoBehaviour
2 months ago
{
[Header("Target")]
2 months ago
[Tooltip("If set, camera will follow this Transform. Otherwise it will auto-find the local player.")]
public Transform target;
2 months ago
[Header("Zoom Settings")]
2 months ago
[SerializeField] private float baseDistance = 6f;
[SerializeField] private float maxDistance = 12f;
[SerializeField] private float zoomSpeed = 5f;
2 months ago
[Header("Follow Settings")]
2 months ago
[SerializeField] private float followSmoothness = 5f;
[SerializeField] private Vector3 heightOffset = new Vector3(0, 3f, 0);
2 months ago
[Header("FOV Boost Settings")]
[SerializeField] private float baseFOV = 60f;
[SerializeField] private float boostFOV = 75f;
[SerializeField] private float fovLerpSpeed = 5f;
2 months ago
private float currentDistance;
private Rigidbody targetRb;
private Camera cam;
private MotionBlur motionBlur;
private bool isBoosting = false;
private void Awake()
{
motionBlur = GetComponent<MotionBlur>();
cam = GetComponent<Camera>();
}
2 months ago
private void Start()
{
2 months ago
Application.targetFrameRate = 120;
currentDistance = baseDistance;
2 months ago
if (target != null)
2 months ago
{
2 months ago
InitializeTarget();
}
transform.parent = null;
2 months ago
}
2 months ago
public void InitializeTarget()
2 months ago
{
2 months ago
targetRb = target.GetComponent<Rigidbody>();
motionBlur.vehicle = target.GetComponent<VehicleController>();
2 months ago
if (targetRb == null)
{
2 months ago
Debug.LogError("CameraController: Assigned target has no Rigidbody component.", this);
2 months ago
enabled = false;
return;
}
2 months ago
Debug.Log("CameraController: Following manually assigned target.", this);
2 months ago
}
private void LateUpdate()
{
2 months ago
if (target == null || targetRb == null) return;
2 months ago
// Dynamic zoom distance based on speed
2 months ago
float speed = targetRb.velocity.magnitude;
2 months ago
float targetDistance = Mathf.Lerp(baseDistance, maxDistance, speed / 30f);
currentDistance = Mathf.Lerp(currentDistance, targetDistance, Time.deltaTime * zoomSpeed);
Vector3 followPos = target.position + heightOffset - target.forward * currentDistance;
2 months ago
transform.position = Vector3.Lerp(transform.position, followPos, Time.deltaTime * followSmoothness);
transform.LookAt(target.position + heightOffset * 0.5f);
// FOV Kick Logic
float targetFOV = isBoosting ? boostFOV : baseFOV;
cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, targetFOV, Time.deltaTime * fovLerpSpeed);
}
public void SetBoosting(bool boosting)
{
isBoosting = boosting;
}
}