Sempre più veloce
Lezione 13 di 14 del corso Gioco in Unity: Snake 3D di Coddy.
Snake diventa più difficile man mano che cresce: ogni cibo accorcia un po' il tick, quindi il serpente si muove più velocemente. Un limite minimo mantiene il gioco giocabile: il tick più breve che può raggiungere.
Mathf.Max restituisce il maggiore tra due numeri, il che lo rende un limite minimo: Mathf.Max(value, floor) non è mai inferiore a floor. Una torretta che spara più velocemente a ogni potenziamento, ma mai più spesso di una volta ogni 0.2 secondi:
cooldown = Mathf.Max(cooldown - 0.05f, 0.2f);Mathf.Min è l'opposto: un limite massimo. Confonderli è un errore facile: con Min, il cooldown qui sopra scenderebbe a 0.2 al primo potenziamento. Il timer in Update() legge tick a ogni frame, quindi un tick più breve viene preso in considerazione a partire dal passaggio successivo.
Sfida
Difficile- Aggiungi
public float speedUp = 0.005f;epublic float minTick = 0.08f;aSnake. - Quando il serpente mangia, riduci
tickdispeedUp, ma mai al di sotto diminTick.
Premi Play e mangia: il serpente accelera. Poi premi Check.
Provalo tu
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Snake : MonoBehaviour
{
public float tick = 0.15f; // secondi tra due passi
public int edge = 7; // le celle della griglia vanno da -edge a edge
public bool alive = true;
public List<Transform> tail = new List<Transform>();
public Transform segmentPrefab;
private float timer;
private Vector3 direction = Vector3.right; // la direzione in cui si è mossa la testa nell'ultimo passo
private Vector3 nextDirection = Vector3.right; // la svolta in attesa del prossimo passo
void Update()
{
if (!alive) return;
Keyboard keyboard = Keyboard.current;
if (keyboard != null)
{
if (keyboard.upArrowKey.wasPressedThisFrame || keyboard.wKey.wasPressedThisFrame) Turn(Vector3.forward);
if (keyboard.downArrowKey.wasPressedThisFrame || keyboard.sKey.wasPressedThisFrame) Turn(Vector3.back);
if (keyboard.leftArrowKey.wasPressedThisFrame || keyboard.aKey.wasPressedThisFrame) Turn(Vector3.left);
if (keyboard.rightArrowKey.wasPressedThisFrame || keyboard.dKey.wasPressedThisFrame) Turn(Vector3.right);
}
timer += Time.deltaTime;
if (timer >= tick)
{
timer -= tick;
Step();
}
}
void Turn(Vector3 newDirection)
{
if (newDirection != -direction)
{
nextDirection = newDirection;
}
}
void Step()
{
direction = nextDirection;
Vector3 next = transform.position + direction;
if (Mathf.Abs(next.x) > edge || Mathf.Abs(next.z) > edge)
{
Die();
return;
}
for (int i = 0; i < tail.Count - 1; i++)
{
if (tail[i].position == next)
{
Die();
return;
}
}
Vector3 tailEnd = tail.Count > 0 ? tail[tail.Count - 1].position : transform.position;
for (int i = tail.Count - 1; i > 0; i--)
{
tail[i].position = tail[i - 1].position;
}
if (tail.Count > 0)
{
tail[0].position = transform.position;
}
transform.position = next;
if (GameManager.Instance.IsFood(next))
{
tail.Add(Instantiate(segmentPrefab, tailEnd, Quaternion.identity));
GameManager.Instance.Eat();
}
}
public bool Occupies(Vector3 cell)
{
if (transform.position == cell) return true;
foreach (Transform segment in tail)
{
if (segment.position == cell) return true;
}
return false;
}
void Die()
{
alive = false;
GameManager.Instance.GameOver();
}
}
Questa lezione include un breve quiz. Inizia la lezione per rispondere e tenere traccia dei tuoi progressi.