Files
gold_dolphin/unity/Assets/2.5D Engine/Scripts/CameraFollow.cs
T
2026-07-05 20:07:30 +08:00

36 lines
1.5 KiB
C#

using UnityEngine;
namespace IndianOceanAssets.Engine2_5D
{
// This class makes the camera follow a target transform with a specified offset.
public class CameraFollow : MonoBehaviour
{
// The target Transform that the camera will follow.
[SerializeField] private Transform target;
// The positional offset from the target.
[SerializeField] private Vector3 offset;
[Tooltip("跟随平滑度(值越大跟随越紧密,10=几乎即时跟随)")]
[SerializeField] private float followSmoothness = 20f;
// Called once per frame — 使用 LateUpdate 确保在 FixedUpdate 物理移动之后执行,
// 与 LightMaskSystem 的 LateUpdate 同步,避免光照抖动
private void LateUpdate()
{
// If a target is assigned, smoothly move the camera towards the target's position plus the offset.
if (target)
transform.position = Vector3.Lerp(transform.position, target.position + offset, Time.deltaTime * followSmoothness);
}
/// <summary>
/// 动态设置跟随目标(由 GameSpawnManager 调用)。
/// </summary>
public void SetTarget(Transform newTarget)
{
target = newTarget;
// 立即跳到目标位置,避免平滑过渡导致的初始位置偏移
if (newTarget != null)
transform.position = newTarget.position + offset;
}
}
}