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
HardStart Survival Night.
GameManager: a singletonInstance,public enum State { Playing, Paused, Won, Lost }andpublic State state.- While Playing, show
Dawn in 12counting downnightLength(rounded up), and spawn a bat at random spawn points everyspawnEveryseconds.Lose()sets Lost. Bat: while Playing, fly at the player atspeed, same height; withinreach, callLose().
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
2Buttons and Screens
Buttons and onClickLambdasWiring in the InspectorPanels On and OffHealth BarsRecap: Upgrade Shop5Saving with PlayerPrefs
Save a NumberLoad with a DefaultHigh ScoreSave SettingsRecap: Coins in the Bank3The Game Manager
One Script Runs the GameThe SingletonGame StatesSwitching on the StateRecap: Round Manager6Project: Endless Runner
Three LanesObstacles Ahead9The New Input System
Two Input SystemsKeyboard and MousePressed This FrameInput ActionsRecap: Input SwapPractice on your own: Unity playground