Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: ben135 on March 29, 2014, 08:09:03 PM

Title: Random Object Spawn
Post by: ben135 on March 29, 2014, 08:09:03 PM
How can I make a certain object (an enemy in this case) spawn at a random rate from the same spot?

Thanks in advance.
Title: Re: Random Object Spawn
Post by: sebaslive on March 30, 2014, 02:27:44 AM
Two states, first one is to create the enemy action, set float variable time to a random float using the action random float, next frame event action to the new state. In state 2 you create a waut action and you set the time to that random float variable, send event back to the first state and now you have a random loop to create enemies.
Title: Re: Random Object Spawn
Post by: ben135 on March 31, 2014, 01:22:06 PM
edit: Which action would I be using to spawn a new character model every time it loops?
Title: Re: Random Object Spawn
Post by: sebaslive on March 31, 2014, 02:21:37 PM
you can use the action create object.

https://hutonggames.fogbugz.com/default.asp?W61
Title: Re: Random Object Spawn
Post by: ben135 on March 31, 2014, 02:51:24 PM
Got it thanks.
Title: Re: Random Object Spawn
Post by: ben135 on March 31, 2014, 05:20:33 PM
Sorry, for double post. I have another question.

I want to have the spawned enemies be deleted/removed when they walk into an invisible wall. Right now I have box collider and rigidbody set up on both objects (enemy character model and the invisible wall) with an FSM on the enemy character model (two states : 1. Trigger Event -> Destroy Self). After it destroys itself for the first time, the timed spawning FSM still runs but no character models actually appear. I see the spawning happen at the spawn spot but it immediately dissapears as if it couldn't find the character model to spawn. How can I solve this? Or maybe there's a better way to delete them as they touch another object?
Title: Re: Random Object Spawn
Post by: sebaslive on March 31, 2014, 05:55:35 PM
Prefabs! So get the enemy and put into a new prefab folder in the project window and that will be your prefab. Place that prefab as the object to create and it will always spawn from there instead of from the game scene (which is why it can't create something that is now gone).
Title: Re: Random Object Spawn
Post by: ben135 on April 01, 2014, 01:41:53 PM
This is perfect. Thanks so much Sebaslive!

Title: Re: Random Object Spawn [SOLVED]
Post by: sebaslive on April 01, 2014, 02:24:00 PM
You're welcome!
Title: Re: Random Object Spawn [SOLVED]
Post by: ben135 on April 01, 2014, 03:56:07 PM
How can I remove all the enemies generated from the spawn with a click?

Right now I have state 1 being activated with a click to state 2 : Activate Game Object action which works for the deactivating the spawn point but not the character models that were generated from the prefab. How can I solve this?
Title: Re: Random Object Spawn
Post by: sebaslive on April 01, 2014, 05:54:13 PM
One way is to set all the prefabs as children to an empty gameObject and destroy that//deactivate that game object to get rid of them all.
Title: Re: Random Object Spawn
Post by: ben135 on April 01, 2014, 06:24:26 PM
How do I point my spawned prefabs as children to a gameObject?
Title: Re: Random Object Spawn
Post by: sebaslive on April 01, 2014, 06:27:54 PM
whenever you create an object it gives you the chance to store that gameObject. After you create it you can go to a new state and set the parent to that gameObject by using set parent.
Title: Re: Random Object Spawn
Post by: ben135 on April 01, 2014, 06:36:43 PM
For some reason I can't set the parent of the prefab to the empty game object. Does it have anything to do with one belonging to the hierarchy (empty game object) and one belonging to assets (prefab)?
Title: Re: Random Object Spawn
Post by: sebaslive on April 01, 2014, 10:53:17 PM
Right, you could set it by using a global variable. You can set the empty gameObject as a global variable and then in the random object that was spawned have the action set parent to that global variable. In the empty gameObject you use the action Get Owner and set it to that global variable.