Menu
CoddyTech

Obstacles Ahead

Part of the Building Real Games section of Coddy's Unity journey. Lesson 28 of 60.

challenge icon

Challenge

Medium

Send obstacles down the road.

  1. In Spawner's Update(), add Time.deltaTime to timer; at spawnEvery, take spawnEvery off and spawn.
  2. Spawn obstaclePrefab at the Spawner's position plus lane * laneWidth along x, with lane from Random.Range(-1, 2).
  3. In Obstacle's Update(), move along Vector3.back at speed units 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

Practice on your own: Unity playground