Obstacles
Lesson 9 of 20 in Coddy's Unity Game: Sky Lanes course.
Now the road needs something to dodge. Every piece gets an obstacle in the middle, in a random lane. A method does it, Fill, called for each new piece in Start and again each time a piece goes back to the front.
The first two pieces stay empty, to give the player a moment: the Road counts the pieces it has filled in made.
void Fill(Transform chunk)
{
made++;
if (made <= safeChunks) return;
int lane = Random.Range(-1, 2);
Place(chunk, barrierPrefab, lane, 0.5f, 0f);
}Random.Range(-1, 2) is -1, 0 or 1: with whole numbers the second number is never picked.
A recycled piece still carries its old obstacle. Clear destroys every child that has a tag. The floor, dashes and edges are Untagged, so they stay:
void Clear(Transform chunk)
{
foreach (Transform child in chunk)
{
if (!child.CompareTag("Untagged")) Destroy(child.gameObject);
}
}Challenge
Easy- In
Roadaddpublic GameObject barrierPrefab;,public int safeChunks = 2;andprivate int made;. - Write
Place(chunk, prefab, lane, y, z): it makes the prefab atchunk.position + new Vector3(lane * laneWidth, y, z)withprefab.transform.rotation, as a child of the piece. - Write
FillandClearas above. - Call
Fill(chunk)for each piece in Start, andClear(chunk); Fill(chunk);when a piece goes to the front.
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