Random Obstacle Generation

Hey all,
How do I make obstacles procedurally generate from the top of the screen (offscreen), and move to the bottom, as illustrated in the image? Similar to asteroids.

You should be able to achieve this using timers and the random function.

You will want to create the objects at a Y position above the screen, so a number like -100 could be enough depending on your game.

For the x value,you will want to know the screen width, for example if it is 800px wide,you could create the objects at position Random(800) which will choose a random position between the x point of 0 and 800.

At beginning of scene - start scene timer spawnObject

If timer spawnObject is 2 - create object Asteroid at X position Random(800) and Y position -100

Reset timer spawnObject

Not at my pc so I cannot test but I achieved this result in a game I was working on previously.

Edit- formatting is a nightmare on mobile :slight_smile:


Thank you! Will this make the objects start at the top and move down?

Oops, forgot about movement :blush:

Anywho, I’m at my pc now. Here’s a sample of code I’ve used to generate objects and move them down.

What this does, it generate the orbs at a random point above the screen, and begins moving them down towards a random position below the screen. Also, the speed is random too. You don’t need to have the speed or destination random, that’s just how it worked in my game.


Thank you so much! I’ll check this out on my computer later

1 Like

hello, sorry to bother again!
i was investigating your code and wanted to see if there was a way for the oranges to go directly down?

You could use the move force to angle (90 degrees for straight down) action to make the object go directly down from its spawn point.

1 Like