29 lines
747 B
C#
29 lines
747 B
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class camfollow : MonoBehaviour
|
|
{
|
|
public Transform target; // 要跟随的目标,这里通常是玩家角色
|
|
public float smoothSpeed = 0.125f; // 摄像机移动的平滑速度
|
|
public Vector3 offset; // 摄像机相对于目标的偏移量
|
|
|
|
void LateUpdate()
|
|
{
|
|
if (target == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// 计算目标位置
|
|
Vector3 desiredPosition = target.position + offset;
|
|
|
|
// 使用平滑插值来移动摄像机
|
|
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
|
|
transform.position = smoothedPosition;
|
|
|
|
// 确保摄像机始终看向目标
|
|
transform.LookAt(target);
|
|
}
|
|
}
|