How to span obstacles 2D runner.

Hi !
I’m currently creating a 2D runner game.


Well I wanted to create a C# script which generate obstacles (2 differents types (for instance big square of small square) : 2 prefabs) from a point (the span empty object) each (randomRange from 0 to 5 seconds).

I’m stuck this is my code :

using UnityEngine;

public class GenerateObstacle : MonoBehaviour {
public GameObject obstacles;

void Start(){

}

void Update(){
   Invoke("GenerateNewObstacle" , Random.Range(5,12));
}

void GenerateNewObstacle()
   {

     Instantiate(obstacles);
   }
}

Thanks for your help !

  1. You will need to create a function that will Spawn the obstacle (See Instantiate in Docs)
  2. Use IEnumerator to create your timer (See IEnumerator in docs) and call your Spawn function
  3. the spawn position will be based on you player’s x and y position

Thanks it works !