设置相机初始变换值,确保原点处于视口中
在 Script 目录下,新建 script 文件,并命名为 CameraController
双击打开、编辑脚本(CameraController.cs)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public GameObject player;
private Vector3 offset; // position 偏移量
// 生命周期函数
// 触发时机:物体被加载到场景时
void Start()
{
// 获取相机与球体的 position 偏移量
offset = transform.position - player.transform.position;
}
// 生命周期函数
// 触发时机:所有 Update 生命周期结束后
void LateUpdate()
{
transform.position = player.transform.position + offset;
}
}
绑定脚本至相机
绑定 GameObject 至脚本
运行项目
网友评论