How to prevent a spawned static mesh from overlaping with another spawned static mesh?

Like the title says. I have an actor#1 that spawns static meshes is random location through the construction script and have another actor#2 that actually spawns the actor #1 constantly. However when i play the game the static mesh sometimes overlaps with each other and i’m trying to figure out how to prevent this from happening but i’m stuck. If anyone can help me out i would greatly appreciate it!

Actor #1 Construction Script -

Actor #2 Spawner -

I’m sure there’s a lot of ways to do this but unless you want to push objects out of your way or stop on collision (sweep) you’d first have to find a valid random location. How to do that depends a lot on the actor your are trying to spawn but for example a simply solution could be to pick a random x,y location then do a box trace to the ground till you hit an object and spawn it there.

I see in your example you provide a const Y value so perhaps you could do your “ground box trace” on Y and if it doesn’t hit 900 then there’s a free space and if it does your hit value would tell you how high things are piled up there and you could choose to place it on top or randomly find another location.

1 Like

I mentioned sweep because another solution could be: check that box then break your sweep hit and on Blocking Hit simply call it again. Granted, you’d likely want to keep attempt count in mind and break out at some point if it takes too long. Not that great of a solution unless you just want something real quick and is likely to pass, hence I mentioned the other solution first :stuck_out_tongue:

Thanks for the suggestion but Im not the best in blueprints so can you give a screenshot of the suggestion you just said. Thanks! Because i don’t really understand fully what you just said.

So this could be improved on but all I did was use the ThirdPersonExample project and added the following blueprint in the ThirdPersonCharacter:

Then when I ran the game I pressed num_1 a few times and produced the following:

I did a Capsule Trace instead of Box trace because I decided to spawn the character at random locations which uses a cylinder for it’s Simple Collision.

1 Like

So this is what i put but the static mesh still continues to overlap with each other. Am i doing something wrong? Also my game is a 2D mobile game so it makes it more harder to work

My example was for 3D objects so for 2D objects simply set the Start Z very high and the End Z very low (instead of picking a random location). Moreover your Half Size should approximate the size of the object you are trying to place; it is currently set to 0,0,0.

Well you see the size of the object I’m putting in the game grows constantly in the game through event tick and there isn’t a set scale. So its kinda of going to be hard setting a half size when the size of the object changes in game. How would set a half size in this case

The Half Size should approximate the size of the object you are trying to place, not the ones already in the game. You should be able to know the size of the object you are about to place and at worst case you can first spawn it then get it’s size then find a valid location.

I did that, i put the half size as the approximate size of the object witch is 1,1,1 and set Start Z very high and end Z very low but the static meshes still overlap?

1,1,1 is incredibly small; things tend not to work very well on that small of a size. After setting them to much larger values, think 300,300,300 if that still doesn’t work I’d suggest using the default cube meshes and see if it works for that. If it doesn’t, please post some game screenshots and code screenshots.

very nice solution to a very common problem. Thank you!