Build the Road
Lesson 5 of 20 in Coddy's Unity Game: Sky Lanes course.
Until now the runner stood on a plain block named Ground. From here the Road script builds the road, and the block is gone.
The road is made of pieces 20 units long, laid one after another. A piece is an empty GameObject named Chunk, and everything on that piece is its child: the floor, the dashes between the lanes and the two edges. Moving the piece will move all of it.
Transform chunk = new GameObject("Chunk").transform;
chunk.position = new Vector3(0f, 0f, i * chunkLength);
Instantiate(floorPrefab, chunk.position + new Vector3(0f, -0.25f, 0f), Quaternion.identity, chunk);The last argument of Instantiate is the parent. The floor is half a unit thick, so its middle sits a quarter below the road's surface at y 0.
Each lane line is four dashes, 3 long, one every 5 units, at x -laneWidth / 2f and laneWidth / 2f. The edges sit at x -4.1 and 4.1, y 0.05.
Challenge
Easy- In
Roadadd the prefab fieldsfloorPrefab,stripePrefabandedgePrefab(allpublic GameObject), andchunkCount = 7,chunkLength = 20f,laneWidth = 2.5f. The Inspector fills the prefabs. - In
Start, makechunkCountpieces, pieceiat zi * chunkLength, each with its floor. - On each piece, for d from 0 to 3, place two dashes at z
-7.5f + d * 5f, y 0.01, one on each lane line. - Place the two edges.
Try it yourself
using UnityEngine;
public class Runner : MonoBehaviour
{
public float laneWidth = 2.5f;
public float laneSpeed = 16f;
public float jumpHeight = 1.6f;
public float gravity = -40f;
public float slideTime = 0.7f;
public int lane;
private CharacterController controller;
private float fallSpeed;
private float slide;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) lane = Mathf.Max(-1, lane - 1);
if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) lane = Mathf.Min(1, lane + 1);
bool jump = Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space);
if (jump && controller.isGrounded)
{
fallSpeed = Mathf.Sqrt(jumpHeight * -2f * gravity);
StopSlide();
}
if ((Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) && controller.isGrounded)
{
slide = slideTime;
Resize(0.5f);
}
if (slide > 0f)
{
slide -= Time.deltaTime;
if (slide <= 0f) StopSlide();
}
if (controller.isGrounded && fallSpeed < 0f) fallSpeed = -2f;
fallSpeed += gravity * Time.deltaTime;
float targetX = lane * laneWidth;
float x = Mathf.MoveTowards(transform.position.x, targetX, laneSpeed * Time.deltaTime);
Vector3 move = new Vector3(x - transform.position.x, fallSpeed * Time.deltaTime, 0f);
controller.Move(move);
}
void StopSlide()
{
if (transform.localScale.y < 1f) Resize(1f);
slide = 0f;
}
void Resize(float height)
{
controller.enabled = false;
transform.position += Vector3.up * (height - transform.localScale.y);
transform.localScale = new Vector3(1f, height, 1f);
controller.enabled = true;
}
}
All lessons in Unity Game: Sky Lanes
Practice on your own: Unity playground