Placing 2D sprites randomly but only on opaque pixels of the 2Dparent.

Hello!
I am ‘randomly’ generating backgrounds of hills, hills populated with trees.

The hills are a random round shape (after stretching, rotating, flipping randomly of a sprite). This random round shape is then placed in the scene as a 2dSprite. Then I place some tree sprites as children randomly within this 2D hill sprite, however the bounds are rectangular so sometimes the tree will end up floating in the air(usually if a corner-ish pixel is randomly selected).

My solution idea was to somehow check if the randomly selected point has opaque pixels before placing the tree at that location or detect it after and delete all the trees not on opaque pixels.

Is there a way to do this with individual sprites generated during Start(). Im still a beginner-intermediate level, so any feedback on this idea in general would be really appreciated.

Please let me know if I left out any important info,
Thank you

You can see what i mean in this pic, I want to find a way to prevent or remove those floating trees

2859871--209228--TreesAndHills.png

Store the Renderer bounds of each placed tree in a List as it’s placed, then check the newly placed tree doesn’t overlap any of the bounds that exist in the list using the intersect method.

1 Like