Create object in random position within an area

Is there a way to create an object (in a random position) within an object?

Declare these variables as object variables for Object1 and set their values:

  • MarginW = Object1.Width() / 2
  • MarginH = Object1.Height() / 2

Create Object1 at position 0;0. Then change the center position to this:

  • X: RandomFloatInRange(Object2.BoundingBoxLeft() + Object1.MarginW, Object2.BoundingBoxRight() - Object1.MarginW)
  • Y: RandomFloatInRange(Object2.BoundingBoxTop() + Object1.MarginH, Object2.BoundingBoxBottom() - Object1.MarginH)

You can remove the MarginW and MarginH variables if Object1 is allowed to be created at the edges of Object2.

You could try the spawn extension

1 Like

Hello @insein I’m not sure if what I did is correct.

This is what I did based of your suggestion (I think I didn’t do it right)
Screenshot_2024_0720_024342

What I plan to do is when body2(green fire objects) collides with body (big green square object) it deletes body2 and creates an object inside condense (purple box)

This is what happens

Hey @RMDB is there a way to only spawn not periodically? I only want to spawn everytime I collide something with the object.

@insein @RMDB It works now! :sweat_smile: thank you both.

1 Like