Faster and Faster
Lesson 13 of 14 in Coddy's Unity Game: Snake 3D course.
Snake gets harder as it grows: each food makes the tick a little shorter, so the snake steps faster. A floor keeps the game playable: the shortest tick it may reach.
Mathf.Max returns the larger of two numbers, which makes it a floor: Mathf.Max(value, floor) is never below floor. A turret that fires faster with each upgrade, but never more often than every 0.2 seconds:
cooldown = Mathf.Max(cooldown - 0.05f, 0.2f);Mathf.Min is the opposite, a ceiling. Mixing them up is an easy slip: with Min, the cooldown above would drop to 0.2 at the first upgrade. The timer in Update() reads tick on every frame, so a shorter tick counts from the next step on.
Challenge
Hard- Add
public float speedUp = 0.005f;andpublic float minTick = 0.08f;toSnake. - When the snake eats, make
tickshorter byspeedUp, but never shorter thanminTick.
Press Play and eat: the snake speeds up. Then press Check.
Try it yourself
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Snake : MonoBehaviour
{
public float tick = 0.15f; // seconds between two steps
public int edge = 7; // the board's cells run from -edge to edge
public bool alive = true;
public List<Transform> tail = new List<Transform>();
public Transform segmentPrefab;
private float timer;
private Vector3 direction = Vector3.right; // the way the head moved on its last step
private Vector3 nextDirection = Vector3.right; // the turn waiting for the next step
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();
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.