Volver a jugar
Lección 21 de 21 del curso Juego de Unity: Carriles celestes de Coddy.
Después de un choque, el juego debería empezar de nuevo. Al cargar la escena otra vez, todo vuelve a estar como al principio: la carretera, el corredor y el título.
if (over && Input.GetKeyDown(KeyCode.Return))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}Va antes de if (!running) return;: después de un choque, el juego no está en ejecución, así que el código que aparece debajo de esa línea nunca se ejecuta. La mejor puntuación está en PlayerPrefs, así que se conserva después de volver a cargar.
Desafío
FácilDespués de un choque, salta para volver a iniciar el juego.
- En
GameManager, agregausing UnityEngine.SceneManagement;. - En Update, antes de
if (!running) return;, sioveres verdadero, al presionar Space o W se llama aSceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);. - En GameOver, termina el mensaje con
+ "\nJump to run again".
Presiona «Jugar», choca y luego presiona Space: vuelve a aparecer el título. Después, «Comprobar».
Pruébalo tú mismo
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);
}
}
}
Esta lección incluye un breve cuestionario. Empieza la lección para responderlo y registrar tu progreso.
Todas las lecciones de Juego de Unity: Carriles celestes
Practica por tu cuenta: Playground de Unity