Help with Random parts system

Hello developers, Does anyone know how I could make a system of appearing parts in random places without the parts hitting a wall?

What I don’t want to happen:


What I want to achieve: :smiley:

The truth is that I have no idea how to start making such a system if someone has a solution or someone who can explain a little how to do it, I would appreciate it a lot :smile:

There’s a lot of ways you can go about doing this, and some ways are better than others.

One more straight forward solution could be to shoot a ray down from above the maze to below it. If it hits a wall, then try again.

Once you hit the ground, shoot a ray in each direction to check how close the wall is. To make sure it’s even possible for the part to fit, add the distance of rays you shot in opposite direction. It should be greater than your part size. For example, if you take the distance from where the North ray hit and from where the South ray hit, it should be bigger or equal to the part size in that direction. Do the same for East and West.

Once you confirm it can fit by doing above, the next step is to make sure it doesn’t hit the walls. The distance must be equal or greater than half of the part size in that direction. If it’s not, you can move the origin point in that direction. I would recommend shooting the ray again after you move it once to make sure it can still fit.

Another option, but a more mathematical option, is to take your maze and transform all the parts into 2D. You can do some collision math to see if a part can fit in a certain random area. You’ll have to do a lot more to make it better, so probably the first solution is the easiest and quickest way.

1 Like

Thank you! I will try to do the first option I think it is also better, thanks for your help

1 Like