Random obstacles spawn in 2D side endless runner game ?

Hello there.

I am new to Unity. I have developed scrolling backgrounds, floor, animation jump, slide and other things. The only thing left for now is an algorithm to spawn random objects, obstacles (I have created 3) that player has to dodge. My game has no platforms, it’s all flat, so I don’t need a random platform generator or similar.

Is there any idea, video tutorial or anything ? I am also new to programming C#, so it would be nice if you could explain with more details ? I am so eager to learn this!

I also checked for other similar questions, but couldn’t find anything specific to my question. Also I seen some other tutorials, but they are mostly for 3D or random platform generators.

How about making a cube gameobject and name it obstacles.
Then flatten it on the y axes and expand it on the x axes, but make the cube invisible.
Then add this script

 public float yDistance = 10;
 public float minSpread = 5;
 public float maxSpread = 10;
 
 public Transform playerTransform;
 public Transform obstaclePrefab;
 
 float ySpread;
 float lastYPos;
 
 void Start(){
     lastYPos = Mathf.NegativeInfinity;
     ySpread = Random.Range(minSpread, maxSpread);
 }
 
 void Update () {
     if(playerTransform.position.y - lastYPos >= ySpread){
         float lanePos = Random.Range(0, 3);
         lanePos = (lanePos-1)*1.5f;
         Instantiate(obstaclePrefab, new Vector3(lanePos, playerTransform.position.y + yDistance, 0), Quaternion.identity);
         
         lastYPos = playerTransform.position.y;
         ySpread = Random.Range(minSpread, maxSpread);
     }
 }

Hope it helps!

If you know how to generate random platforms then you know how to spawn random objects/obstacles. Create obstacles in the same way as platforms with different positions and different scripts attached to them according to their behaviour.

Basic approach is to make prefab of obstacle gameobject and instantiate them on run time in similar way as you will instantiate platforms.