- 脚本可以挂到相机上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform player; // 跟随的游戏对象
private Vector3 cameraOffset; // 相机的偏移
[Range(0.01f,1.0f)] // 相机平滑度范围
public float smoothness = 0.5f; // 初始平滑度为0.5;
void Start()
{
cameraOffset = transform.position - player.transform.position;
// 相机偏移位置等于相机位置减去玩家位置。
}
void Update()
{
Vector3 newPos = player.position + cameraOffset; // 新的位置等于玩家位置加上相机位置
transform.position = Vector3.Slerp(transform.position, newPos, smoothness);
// 相机的位置更新相机在a点上随着玩家b点的移动和玩家保持一样的距离设定相机偏移时的速度。
}
}