playMaker

Author Topic: Spawning Objects Randomly Without Overlapping  (Read 2942 times)

misterjuly

  • Sr. Member
  • ****
  • Posts: 334
Spawning Objects Randomly Without Overlapping
« on: June 07, 2021, 10:58:28 PM »
Hello,

I was able to figure out how to randomly spawn in objects at random points on a plane, but I have a problem. Sometimes the objects overlap. I tried adding a collision2d event action or a trigger action with a collider but it didn't seem to work. Do clone objects register differently as I am cloning them. Thanks!

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: Spawning Objects Randomly Without Overlapping
« Reply #1 on: June 08, 2021, 04:11:58 AM »
First, check your collision action matrix:

https://docs.unity3d.com/Manual/CollidersOverview.html

Second, use Collision/Trigger On Stay, not enter, not exit. Should work fine.
Available for Playmaker work

misterjuly

  • Sr. Member
  • ****
  • Posts: 334
Re: Spawning Objects Randomly Without Overlapping
« Reply #2 on: June 08, 2021, 08:29:10 AM »
Thanks for your reply. I checked the on collision stay issue and that was not the problem. I wasn't really sure how to interpret the link you sent, so I wasn't sure what to do with that; a bit more explanation would be helpful :)

I also just realized that other colliders are not working right. And it's not from cloning as I tested individual colliders by themselves. I've made test projects before with colliders so I'm really not sure of what the issue is. Perhaps it's something with that link I couldn't understand. Anyways, just thought I'd give you more detail. Thanks again!

John Bassi

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: Spawning Objects Randomly Without Overlapping
« Reply #3 on: June 08, 2021, 04:11:32 PM »
Put some breakpoonts and see if the collisions are actually happening. Do objects have rigidbody  and what type of rigidbodies are they?
Available for Playmaker work

misterjuly

  • Sr. Member
  • ****
  • Posts: 334
Re: Spawning Objects Randomly Without Overlapping
« Reply #4 on: June 08, 2021, 06:53:17 PM »
I have rigid bodies on there. They are Kinematic so that the objects don't fall. I don't know how to check to see if the objects are colliding because I have objects randomly spawning and as a result it loops through too quickly (sorry if I'm not understanding how to do it right). I know some collisions are working, its just when I spawn in the objects it doesn't seem to work. Heres my FSM. Thanks!

ToxicFrog

  • Beta Group
  • Full Member
  • *
  • Posts: 139
Re: Spawning Objects Randomly Without Overlapping
« Reply #5 on: June 09, 2021, 08:41:08 AM »
If I'm reading your flow correctly, it looks like you're testing for collision before the object is created. So, there's nothing for it to collide with.
I'd suggest you create the object, and if it collides with another of the specified type, despawn (or destroy) the object and loop back around to do it again.

Also, I recommend you use pools to spawn/despawn rather than creating objects - it is MUCH faster and more efficient! Look for Path-o-logical Games' Pool Manager - there are PM actions for it (on the ecosystem, I believe)

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: Spawning Objects Randomly Without Overlapping
« Reply #6 on: June 09, 2021, 09:25:00 AM »
Yeah, you first need to create it. If you don't want it to be seen, create it with renderer and all the other stuff disable, just leave the collider and rigidbody on, if everything is ok, then turn on the renderer and everything else.

Also, +1 for pooling. Beware that you need to reset all the variables and everything when using pooling to bring the object to initial state when reusing it.
Available for Playmaker work

ToxicFrog

  • Beta Group
  • Full Member
  • *
  • Posts: 139
Re: Spawning Objects Randomly Without Overlapping
« Reply #7 on: June 09, 2021, 11:17:38 AM »
Alternatively, you could test to see if an object with the same tag is within a certain distance of your Vector2, if so, don't spawn and re-roll your vector2. Test again - if it's not within distance of the tagged object, proceed with spawining.

misterjuly

  • Sr. Member
  • ****
  • Posts: 334
Re: Spawning Objects Randomly Without Overlapping
« Reply #8 on: June 10, 2021, 04:42:37 PM »
ToxicFrog, I'm trying your second approach, but would I use get distance action? There is no way to check for a tag, only another game object. How would I check for a tag instead of another game object? Sorry for all the questions. Thanks!

ToxicFrog

  • Beta Group
  • Full Member
  • *
  • Posts: 139
Re: Spawning Objects Randomly Without Overlapping
« Reply #9 on: June 10, 2021, 10:59:13 PM »
Maybe try this:

1. Have an empty game object 'SpawnPoint'

2. Create random Vector

3. Position SpawnPoint at Vector

4. 'Find Closest' Action: Game Object='SpawnPoint' and tag 'ObjectTag'. Store Object='ClosestObject', Store Distance='Distance_FLOAT'

5. Float Compare (Float1=Distance_FLOAT, Float2=MinimumSafeDistance). If LessThan, Goto Step 2. If Equal or GreaterThan, Spawn Object at SpawnPoint.

Hope that makes sense!
« Last Edit: June 11, 2021, 09:41:50 AM by ToxicFrog »

misterjuly

  • Sr. Member
  • ****
  • Posts: 334
Re: Spawning Objects Randomly Without Overlapping
« Reply #10 on: June 11, 2021, 09:54:33 AM »
I think that worked! Thanks! Although one question: I realize this is very unlikely, but what if the game loops over 1000 times trying to place the object and stops working? Is there a way to allow it to loop as many times as it needs to without stopping? Thanks for all of your help!

ToxicFrog

  • Beta Group
  • Full Member
  • *
  • Posts: 139
Re: Spawning Objects Randomly Without Overlapping
« Reply #11 on: June 11, 2021, 10:36:47 AM »
Add step 2.5:  NextFrame event. That will prevent the 1000 loops lockup

misterjuly

  • Sr. Member
  • ****
  • Posts: 334
Re: Spawning Objects Randomly Without Overlapping
« Reply #12 on: June 11, 2021, 01:03:36 PM »
I think that worked! Thanks SO much!