Menu
CoddyTech

标题画面

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

游戏现在有了开始界面:打开游戏时,会显示标题、游戏名称、最高分和“跳跃以运行”。道路保持静止,在跳跃键(Space 或 W)开始游戏前,按键都不起作用。

除了 over,还需要第二个状态:running。它初始为 false,跳跃会将它设为 true,而 GameOver 会再次将它设为 false。消息中的 \n 会另起一行:

messageText.text = "My Game\nBest: " + PlayerPrefs.GetInt("BestScore", 0) + "\nPress to start";

Update 中所有会让游戏移动的内容都放在 if (!running) return; 之后,这样在标题界面上就不会有任何东西移动。

challenge icon

挑战

中等

让游戏启动时显示标题画面,并等待跳跃输入。

  1. 在 GameManager 中添加 public bool running;。Start 将 speed = 0f 设为初始值,并设置 messageText.text = "Sky Lanes\nBest: " + PlayerPrefs.GetInt("BestScore", 0) + "\nJump to run";。
  2. 在 Update 中,当 !running && !over 时按下 Space 或 W,会将 running = true,并显示空消息;接着执行 if (!running) return;。
  3. 让 GameOver 以 if (!running) return; 和 running = false; 开始。
  4. 在 Runner 中,只有当 game.running 为真时才读取按键和触发器。

按下“运行”:标题画面会一直等待,直到你跳跃。然后按“检查”。

亲手试试

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 float magnetTime = 8f;

    public int lane;
    public float magnet;
    public bool shield;

    private CharacterController controller;
    private GameManager game;
    private Renderer body;
    private float fallSpeed;
    private float slide;
    private Color color;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        game = FindFirstObjectByType<GameManager>();
        body = GetComponent<Renderer>();
        color = body.material.color;
    }

    void Update()
    {
        if (!game.over)
        {
            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 (magnet > 0f) magnet -= Time.deltaTime;

        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);

        body.material.color = shield ? new Color(0.3f, 0.9f, 1f) : color;
        ShowPower();
    }

    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;
    }

    void ShowPower()
    {
        string text = "";
        if (magnet > 0f) text = "Magnet " + Mathf.CeilToInt(magnet);
        if (shield) text += (text == "" ? "" : "   ") + "Shield";
        game.powerText.text = text == "" ? " " : text;
    }

    void OnTriggerEnter(Collider other)
    {
        if (game.over) return;
        if (other.CompareTag("Obstacle"))
        {
            if (shield)
            {
                shield = false;
                Destroy(other.gameObject);
                return;
            }
            game.GameOver();
        }
        else if (other.CompareTag("Coin"))
        {
            game.AddCoin();
            Destroy(other.gameObject);
        }
        else if (other.CompareTag("Magnet"))
        {
            magnet = magnetTime;
            Destroy(other.gameObject);
        }
        else if (other.CompareTag("Shield"))
        {
            shield = true;
            Destroy(other.gameObject);
        }
    }
}

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

自己练习: Unity 在线编辑器