Obstacles Ahead
Part of the Building Real Games section of Coddy's Unity journey. Lesson 28 of 60.
Challenge
MediumSend obstacles down the road.
- In
Spawner'sUpdate(), addTime.deltaTimetotimer; atspawnEvery, takespawnEveryoff and spawn. - Spawn
obstaclePrefabat the Spawner's position pluslane * laneWidthalong x, withlanefromRandom.Range(-1, 2). - In
Obstacle'sUpdate(), move alongVector3.backatspeedunits a second; destroy it below z-10.
Press Play: obstacles come in all three lanes. Then Check.
Try it yourself
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject obstaclePrefab;
public float spawnEvery = 1.2f;
public float laneWidth = 2.5f;
private float timer;
// Every spawnEvery seconds, send an obstacle down a random lane
}
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