再玩一次
Coddy Unity 游戏:天空赛道 课程的第 21 课,共 21 课。
发生碰撞后,游戏应该重新开始。重新加载场景会让所有内容恢复到开始时的状态:道路、跑者和标题。
if (over && Input.GetKeyDown(KeyCode.Return))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}这段代码要放在 if (!running) return; 之前:发生碰撞后,游戏不会运行,因此这一行下面的代码永远不会运行。最高分保存在 PlayerPrefs 中,所以重新加载后仍会保留。
挑战
简单撞车后,按下跳跃键即可重新开始游戏。
- 在
GameManager中添加using UnityEngine.SceneManagement;。 - 在 Update 中,在
if (!running) return;之前,如果over为真,按下 Space 或 W 时调用SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);。 - 在 GameOver 中,将消息结尾设为
+ "\nJump to run again"。
按下“开始”,撞车,然后按 Space:标题会重新显示。接着按“检查”。
亲手试试
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.running)
{
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.running) 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 在线编辑器