How to randomly spawn coins in level?

So I’m creating a game and I want to spawn coins at random spots in the level as soon as the game starts.

As simple as it seems, I can’t seem to find a solution on YouTube or here in the community.

Some tutorials require that you manually add null objects to be used as spawn points. I can’t do that because I’d have to create thousands of points all across the level.

It might be possible to randomly generate points in the level but I need those points to work in three dimensions. There are hills, platforms, stairs, etc. and I need coins to generate in all of those places.

How can I go about doing this?

Hm, you could start looking into how Unreals Procedural Foliage Tool. I think it does exactly what you are trying to do.

Another way would be this algorithm:

  1. generate a point in a 3d sphere (using random unit vector and max radius).
  2. then check if the point is valid (check it is not inside of geometry).
  3. check if the point is too high above the ground (do a trace in directon FVector(0,0,-1))
  4. if it is, place it closer to the ground.
  5. repeat

Keep in minde that this simple algorithm creates more points closer to the center. You need something more sophisticated if you want lot’s of coins.

Hello, Ken,

There are ways to spawn random actors at random locations in the level. The thing is that you need some way to tell the engine what areas you are interested to spawn in, so you can get interesting content that matches what you are imagining as a designer. Absolutely random location will seem just like that, random. And where is the level design in that? An ultra smart system that accounts for all your level variables will be very complicated to produce and probably harder and less flexible than just putting some manual areas of interest in your level.

That doesn’t mean that you need a spawn point per coin either. There are many ways to go around this. You can create volumes (think of them as spawn volumes instead of points) inside of which your coins will apear at random locations, maybe with fixed distances between them (so like a grid). But now you are spawning at specially designated areas.

Inside those areas, you can do traces into the ground, so you can get the exact location at which you want to spawn, and also maybe compare the impact normal so you can limit your spawns to only appear when the ground is flat, or maybe within a slope that is not inaccessible to the player.

You can also build your spawn algorithm in a way that it has a bias to the center, or the edges of the area. Maybe so it tries to make lines on a certain axis once it chooses a starting point.

Another way to go around this would be to have a spawn actor that has a lot of spline components inside. You could put your splines manually along the paths where you might want your coins to appear, so you can manually design the shape of those potential coins within the level. Then you get chose some random indexes out of an array made out of your spline components, and spawn coins at certain distances along the chosen splines. Maybe combining that with a trace to the floor so you don’t have to be as careful with their location and can deploy them a bit faster.

Finally, you can do an all random spawn actor, that gets a lot of locations within your level, makes some tests to check if they are valid, and spawns at those points, either single coins or clusters. But the shapes and interesting variations that you get out of that will be completely dependent on the complexity of your universal spawn actor. For things that are only secondary set dressing and that don’t need to have a lot of control (a trash placer, for example) this could work quite well.

The key for the flexibility you are looking for is doing a trace to the ground, and starting it from a point that you know is above your level. Doing this, any point that you generate in any way will work for you because you will spawn at the location corresponding vertically to your random point but at ground height. And also testing the location against some parameters (what is the slope angle? am I colliding with something different than the floor?) that will help you further refine your generated locations.

In any case, you will have a spawn actor. It doesn’t have to be a point, it can be an area defined by a collision box, a series of splines, or even an actor which location doesn’t matter at all. There can be a lot of spawners in your level, or you can have only one with different components defining your areas inside. Eventually you will get some points, at which you will call the “Spawn actor of class”, with a reference to the class of your coin.

I hope this helps to give an overview on the many ways to accomplish this. In the end, it all comes down to your implementation.

Thank you for the information! I know it must have taken a while to get that all written down. You’ve definitely given me some ideas to try. Hopefully I can get this thing running. :slight_smile: