Menu
CoddyTech

回顾:炮塔

本课属于 Coddy Unity 学习旅程的“物理与游戏玩法”单元。第 46 课,共 71 课。

本章回顾将本章内容串联起来:将方向投影到地面上、使用 Quaternion.LookRotation、通过 RotateTowards 平稳转向,以及旋转偏移量。

challenge icon

挑战

中等

编写炮塔:它会追踪坦克并发射炮弹。

  1. 每帧,将 tank.position - transform.position 的 y 设为 0,然后使用 Quaternion.RotateTowards 朝向它的 Quaternion.LookRotation 方向旋转,旋转速度为 turnSpeed * Time.deltaTime。
  2. 按下 Space 时,在 transform.position + transform.rotation * new Vector3(0, 0.5f, 1) 处生成 shellPrefab,并使用 transform.rotation。

按下 Play,驾驶坦克并开火。然后点击 Check。

亲手试试

using UnityEngine;

public class Turret : MonoBehaviour
{
    public Transform tank;
    public GameObject shellPrefab;
    public float turnSpeed = 90f;

    void Update()
    {
    }
}
quiz icon自我测试

这节课包含一个小测验。开始学习即可作答,并记录你的进度。

物理与游戏玩法中的所有课

自己练习: Unity 在线编辑器