Schermata del titolo
Lezione 20 di 21 del corso Gioco Unity: Corsie nel cielo di Coddy.
Il gioco ha un inizio: si apre con una schermata del titolo che mostra il nome, il punteggio migliore e «Salta» per iniziare. La strada resta ferma e i tasti non fanno nulla finché un tasto per saltare (Space o W) non avvia la partita.
Serve un secondo stato accanto a over: running. Inizia come false, un salto lo imposta su true e GameOver lo reimposta su false. Un \n nel messaggio inizia una nuova riga:
messageText.text = "My Game\nBest: " + PlayerPrefs.GetInt("BestScore", 0) + "\nPress to start";Tutto ciò che in Update sposta il gioco viene dopo if (!running) return;, così nella schermata del titolo non si muove nulla.
Sfida
MedioApri il gioco su una schermata iniziale che attende un salto.
- In
GameManageraggiungipublic bool running;. Start impostaspeed = 0femessageText.text = "Sky Lanes\nBest: " + PlayerPrefs.GetInt("BestScore", 0) + "\nJump to run";. - In Update, Space o W mentre
!running && !overimpostarunning = truee un messaggio vuoto; poiif (!running) return;. - GameOver inizia con
if (!running) return;erunning = false;. - In
Runner, leggi i tasti e i trigger solo mentregame.running.
Premi «Gioca»: la schermata iniziale attende finché non salti. Poi premi «Verifica».
Provalo tu
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);
}
}
}
Tutte le lezioni di Gioco Unity: Corsie nel cielo
Esercitati da solo: Playground Unity