Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: misterjuly 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!
-
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.
-
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
-
Put some breakpoonts and see if the collisions are actually happening. Do objects have rigidbody and what type of rigidbodies are they?
-
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!
-
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)
-
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.
-
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.
-
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!
-
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!
-
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!
-
Add step 2.5: NextFrame event. That will prevent the 1000 loops lockup
-
I think that worked! Thanks SO much!