Files
decay-grid/Assets/Scripts/Infrastructure/Unity/CameraController.cs

54 lines
1.7 KiB
C#

using KBCore.Refs;
using UnityEngine;
namespace Infrastructure.Unity
{
public class CameraController : MonoBehaviour
{
[SerializeField] private Transform target;
[SerializeField] private Vector3 offset = new(-20, 20, -20);
[SerializeField] private float smoothSpeed = 5f;
private Vector3 _originalPos; // This field is declared but not used in the provided LateUpdate logic.
private float _shakeTimer;
private float _shakeMagnitude;
private void LateUpdate()
{
if (!target) return;
// We follow the player, but we might want to clamp X/Z movement
// so the camera doesn't jitter too much, only tracking Y (falling).
// For a dynamic feel, let's track everything loosely.
var targetPos = target.position + offset;
var smoothPos = Vector3.Lerp(transform.position, targetPos, smoothSpeed * Time.deltaTime);
if (_shakeTimer > 0)
{
_shakeTimer -= Time.deltaTime;
var shakeOffset = Random.insideUnitSphere * _shakeMagnitude;
transform.position = smoothPos + shakeOffset;
}
else
{
transform.position = smoothPos;
}
}
public void SetTarget(Transform newTarget)
{
target = newTarget;
}
public void Shake(float duration, float magnitude)
{
_shakeTimer = duration;
_shakeMagnitude = magnitude;
}
public void ShakeLight() => Shake(.1f, .1f);
public void ShakeMedium() => Shake(.15f, .3f);
public void ShakeHeavy() => Shake(.1f, .5f);
}
}