回顾:追逐摄像机
本课属于 Coddy Unity 学习旅程的“物理与游戏玩法”单元。第 51 课,共 71 课。
本次回顾将本章内容串联起来:用 SmoothDamp 平滑处理角度,围绕目标旋转偏移量,并对前方的一个点使用 LookAt。
挑战
中等编写追逐摄像机:它会稍晚一点转到汽车后方。
- 将摄像机的
yaw保存在字段中,在Start()中将其设为car.heading,并在每次LateUpdate()中用Mathf.SmoothDamp(yaw, car.heading, ref yawVelocity, smoothTime)让它平滑变化。 - 将它放在
car.transform.position + Quaternion.Euler(0, yaw, 0) * offset的位置。 - 使用
transform.LookAt(car.transform.position + car.transform.forward * ahead);瞄准。
按“开始”,绕圈驾驶。然后按“检查”。
亲手试试
using UnityEngine;
public class ChaseCamera : MonoBehaviour
{
public Car car;
public Vector3 offset = new Vector3(0, 3, -6);
public float smoothTime = 0.3f;
public float ahead = 4f;
void LateUpdate()
{
}
}
这节课包含一个小测验。开始学习即可作答,并记录你的进度。
物理与游戏玩法中的所有课
自己练习: Unity 在线编辑器