Menu
CoddyTech

建造道路

Coddy Unity 游戏:天空赛道 课程的第 5 课,共 21 课。

到目前为止,跑步者站在一个名为 Ground 的普通方块上。从这里开始,Road 脚本会建造道路,而这个方块也就不再使用了。

道路由一段段长 20 个单位的路段首尾相接组成。每段路都是一个名为 Chunk 的空 GameObject,这段路上的所有内容都是它的子对象:路面、车道之间的虚线和两侧边缘。移动这段路时,所有内容都会一起移动。

Transform chunk = new GameObject("Chunk").transform;
chunk.position = new Vector3(0f, 0f, i * chunkLength);
Instantiate(floorPrefab, chunk.position + new Vector3(0f, -0.25f, 0f), Quaternion.identity, chunk);

Instantiate 的最后一个参数是父对象。路面厚半个单位,因此它的中心位于道路表面(y 为 0)下方四分之一个单位处。

每条车道线由 4 条长 3 个单位的虚线组成,每隔 5 个单位一条,分别位于 x -laneWidth / 2f 和 laneWidth / 2f 处。两侧边缘位于 x 为 -4.1、y 为 0.05 的位置。

challenge icon

挑战

简单

搭建道路。

  1. 在 Road 中添加 public GameObject floorPrefab, stripePrefab, edgePrefab;、chunkCount = 7、chunkLength = 20f 和 laneWidth = 2.5f。
  2. 在 Start 中创建 chunkCount 个名为 Chunk 的空 GameObject,位置设为 z i * chunkLength,并为每个 GameObject 添加地面。
  3. 在每个路段上,让 d 从 0 到 3,在每条车道线处放置一段虚线,z 坐标为 -7.5f + d * 5f,y 坐标为 0.01。
  4. 然后在 x 坐标 -4.1 和 4.1、y 坐标 0.05 处放置道路边缘。

按“开始”:你会看到一条由七段组成、带有虚线车道线的道路。然后点击“检查”。

亲手试试

using UnityEngine;

public class Runner : MonoBehaviour
{
    public float laneWidth = 2.5f;
    public float laneSpeed = 16f;
    public float jumpHeight = 1.6f;
    public float gravity = -40f;
    public float slideTime = 0.7f;

    public int lane;

    private CharacterController controller;
    private float fallSpeed;
    private float slide;

    void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) lane = Mathf.Max(-1, lane - 1);
        if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) lane = Mathf.Min(1, lane + 1);
        bool jump = Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space);
        if (jump && controller.isGrounded)
        {
            fallSpeed = Mathf.Sqrt(jumpHeight * -2f * gravity);
            StopSlide();
        }
        if ((Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) && controller.isGrounded)
        {
            slide = slideTime;
            Resize(0.5f);
        }

        if (slide > 0f)
        {
            slide -= Time.deltaTime;
            if (slide <= 0f) StopSlide();
        }

        if (controller.isGrounded && fallSpeed < 0f) fallSpeed = -2f;
        fallSpeed += gravity * Time.deltaTime;

        float targetX = lane * laneWidth;
        float x = Mathf.MoveTowards(transform.position.x, targetX, laneSpeed * Time.deltaTime);
        Vector3 move = new Vector3(x - transform.position.x, fallSpeed * Time.deltaTime, 0f);
        controller.Move(move);
    }

    void StopSlide()
    {
        if (transform.localScale.y < 1f) Resize(1f);
        slide = 0f;
    }

    void Resize(float height)
    {
        controller.enabled = false;
        transform.position += Vector3.up * (height - transform.localScale.y);
        transform.localScale = new Vector3(1f, height, 1f);
        controller.enabled = true;
    }
}

Unity 游戏:天空赛道中的所有课

自己练习: Unity 在线编辑器