Menu
CoddyTech

Survival Night

Part of the Building Real Games section of Coddy's Unity journey. Lesson 59 of 60.

A survivor stands in a dark field, and bats come for them all night, a new one every couple of seconds from one of the field's four corners. Each bat flies straight at the survivor, and the first one to reach them ends the night. A clock at the top counts the seconds to dawn: last until then and you win. A Pause button freezes everything, and when the night ends, Play again starts a new one.

The Player script, which walks the survivor with the Move action, is written. Write GameManager and Bat.

challenge icon

Challenge

Hard

Start Survival Night.

  1. GameManager: a singleton Instance, public enum State { Playing, Paused, Won, Lost } and public State state.
  2. While Playing, show Dawn in 12 counting down nightLength (rounded up), and spawn a bat at random spawn points every spawnEvery seconds. Lose() sets Lost.
  3. Bat: while Playing, fly at the player at speed, same height; within reach, call Lose().

Press Play: bats come. Then Check.

Try it yourself

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;

public class GameManager : MonoBehaviour
{
    public float nightLength = 15f;
    public float spawnEvery = 2f;
    public GameObject batPrefab;
    public Transform[] spawnPoints;
    public TMP_Text dawnText;
    public Button pauseButton;
    public GameObject pausePanel;
    public GameObject endPanel;
    public TMP_Text endText;
    public Button restartButton;
}

All lessons in Building Real Games

Practice on your own: Unity playground