Speeding Up
Part of the Building Real Games section of Coddy's Unity journey. Lesson 29 of 56.
Challenge
MediumThe road speeds up the longer you run. The speed belongs to the whole game, so it moves into GameManager, and every obstacle reads it from there. Keep everything from the earlier lessons.
In GameManager, which the Inspector gives speed (10), speedUp (0.5) and maxSpeed (20):
- Make it a singleton: a
public static GameManager Instance, set to this script inAwake(). - Every frame,
speedgrows byspeedUpper second, and never goes abovemaxSpeed.
In Obstacle:
- Delete the
speedfield and move atGameManager.Instance.speedinstead. Read it every frame, so the obstacles already on the road speed up too.
Press Play and run until the road is fast, then Check.
Try it yourself
using UnityEngine;
public class GameManager : MonoBehaviour
{
// Runs the game: the speed, the distance and the crash. Later lessons fill this in.
}
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 Swap