Generate a random position on a map

  1. What do you want to achieve? Keep it simple and clear!
    I want to generate a position that can be anywhere on the map.
  2. What is the issue? Include screenshots / videos if possible!
    I can’t seem to find any solution to how I can do this.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried dividing the map into a lot of squares but that seems too much for me to handle.

I’m currently working on a game, and ran into a problem. I need to randomly spawn items from the sky so that they fall down onto the map. The test map I made was a square so I achieved that by basically creating a random value from dividing the X and Z size values of the map, made a vector3 and added it to the position of the map. But when my friend built the map, it looked like this:
image
It’s not a square. So I want to know if it’s possible to get a random position that can be anywhere on the entire map, without any spots being left out? If it’s not possible I can move back to using a square map.

Any help is appreciated, thanks in advance.

Not sure if your green area conists of one part, or multiple. If it is multiple I suggest unioning them together and looking at this post How to random position for part in part - #7 by RealistEntertainment. Part is your new part and PartMoving is your already existing part. Sorry if this isn’t what you mean.

That’s the solution I mentioned above but it’s only used for a rectangular, using that with my map, which consists of multiple sides would resulting in the items sometimes spawning out of the map.

Or that you spawn them randomly within a certain pre-set radius from a point, or pick random vectors (Of course within a valid range) and ray-cast downwards and see if there is a ground under that position, and if not, randomize it again until you will find the right Vectors.

2 Likes

That’s actually a good idea, I’ll go try that now. If it works I’m marking this as a solution.

1 Like

If the green space is terrain, then can’t you just create multiple parts covering your whole terrain. Once you union them together you will have a part that is the exact size and shape of your map. Then just use the code. Am I being stupid? DxIsLast’s method is probally better.

1 Like

Executing the code will spawn the items within the radius of a rectangle with the same size of the union, which has many spots that isn’t included in the actual visible map.
image
This image illustrates the radius of the items spawning when that code is used.

1 Like

You aren’t stupid. But the problem with that is getting the point inside that polygon, that’s not out of boundaries. The union itself will be just a block covering the boundaries of that polygon (based on the maximal size on X and Z axes), not the polygon itself.

2 Likes

I just used your method and it worked perfectly, I just had to keep executing the same code until it finds a part directly beneath it then spawn an item. Thanks for the solution.

2 Likes

Makes sense now. Didnt realise

2 Likes

It’s all good. Although your idea is really good, hopefully one day Roblox will change unions to function that way.

1 Like