Three Lanes
Part of the Building Real Games section of Coddy's Unity journey. Lesson 27 of 56.
This chapter builds an Endless Runner. A runner races down a road of three lanes and switches lanes to dodge the obstacles that come toward it. Over the next lessons the road speeds up, the distance shows on screen, a crash ends the run with a button to run again, and the best run is saved. The runner never moves forward: it stays at z = 0 and the obstacles travel toward it, so the road never runs out. The game has four scripts, Runner, Spawner, Obstacle and GameManager, and the last three are empty for now.
A runner in lanes is not steered like a car: it is always in one of a few places. Keep the place as a whole number, change the number on a key press, and work out the position from it. This window cleaner's platform serves the five floors of a building, 3 units apart:
[SerializeField] private float floorHeight = 3f;
[SerializeField] private float moveSpeed = 6f;
private int floor = 0;
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow)) floor++;
if (Input.GetKeyDown(KeyCode.DownArrow)) floor--;
floor = Mathf.Clamp(floor, 0, 4);
Vector3 p = transform.position;
p.y = Mathf.MoveTowards(p.y, floor * floorHeight, moveSpeed * Time.deltaTime);
transform.position = p;
}GetKeyDown makes one press one floor, where GetKey would add a floor on every frame the key is held. Mathf.Clamp keeps the number on the building, so up on the top floor does nothing, and the next press down goes straight to floor 3. Mathf.MoveTowards slides the platform toward its floor at moveSpeed units a second and stops exactly on it, instead of jumping there in one frame. The position is a copy, changed and assigned back, because transform.position.y cannot be set on its own.
Challenge
MediumStart the Endless Runner with the runner. The Inspector gives Runner a laneWidth of 2.5 and a laneSpeed of 15, and lane is -1 for the left lane, 0 for the middle and 1 for the right. In Runner's Update():
- Each press of the left or right arrow changes
laneby one, that way. Holding the key still changes it by one. lanenever goes below -1 or above 1.- The runner slides along x to
lane * laneWidthatlaneSpeedunits per second, and keeps its y and z.
Leave the other three scripts as they are for now. Press Play, click the game and switch lanes, then Check.
Try it yourself
using UnityEngine;
public class Runner : MonoBehaviour
{
public float laneWidth = 2.5f;
public float laneSpeed = 15f;
private int lane = 0; // -1 is the left lane, 0 the middle, 1 the right
void Update()
{
// Change lanes with the arrows, then slide toward the lane's x
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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