Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: 3D Lunatic on October 18, 2019, 05:05:20 AM

Title: Trigger a particle system [SOLVED]
Post by: 3D Lunatic on October 18, 2019, 05:05:20 AM
I bought some assets to create cool magical effects, such as auras or particles. These assets include prefabs which I just need to drag into my scene.

How do I activate such a prefab every time a collider is triggered? Right now it only works the first time my trigger area is entered, but it doesn't repeat. Could someone describe the FSM that I need to activate the particle effect every time? Thanks!
Title: Re: Trigger a particle system
Post by: djaydino on October 18, 2019, 07:06:21 AM
can you show you fsm setup?

the trigger is handled by an fsm or on the prefab?

if fsm :
if you want it to happen only when entered again, then you might need a trigger event and set 'on trigger exit' and loop back to the trigger event 'on trigger enter/stay'
Title: Re: Trigger a particle system
Post by: 3D Lunatic on October 21, 2019, 02:20:16 PM
This is my FSM:
I'm using an animation of bats flying up, which is a particle effect. When the player enters the trigger zone (a rectangle on this object), the bat cloud is released. Then it should wait 10 seconds before releasing them again, so it doesn't happen too often. That's why I put a wait action for 10 seconds after the activate game object action.

I think what I'm doing wrong is triggering the animation using activate game object. Shouldn't there be another way to trigger a particle effect? Thanks for your advice.
Title: Re: Trigger a particle system
Post by: Thore on October 21, 2019, 08:58:08 PM

1. make sure you use the correct suite of actions. If you use 2D physics, e.g. rigidbody2d, collider2d etc, you also need to use physics actions with 2d at the end. The standard (3d) variant is without the suffix.

2. For debug purposes, add a state with key down that also leads to the particle firing state, when pressed, so you can quickly test it and tweak it. If it works well, you can add complexity and trigger it with a collider. Generally, try a partial solution first.

3. The easiest implementation is a single state, and a global system event called OnTriggerEnter (2d). When you add that to another state, and run into the trigger, it should activate. But it has no options. If you want options, you can use the trigger actions.

4. Make sure that the collider is set to trigger.

5. To fire particles, you need “particle” actions from the Ecosystem (free PM addon). I am quite sure I got them there.
Title: Re: Trigger a particle system
Post by: Broken Stylus on October 22, 2019, 05:47:58 AM
Yes, there are actions to control that on PM although they don't cover all the modules.
Of an alternative method, you can also set your shuriken system to "play on awake" (a box to tick in the main module) and put a duration of ten seconds. So then you just have to activate the game object that controls the particles, let it loop every ten seconds if you want and just deactivate the game object when you're done.
Title: Re: Trigger a particle system
Post by: djaydino on October 22, 2019, 07:21:17 AM
On the Ecosystem ( you can find several custom actions for the particle system.
One of them is called 'Particle System Play' that one you can use to play a particle.
Title: Re: Trigger a particle system
Post by: 3D Lunatic on October 28, 2019, 06:50:37 AM
Thanks for your suggestions. I'm running into one more problem with this:
I have the FSM on an empty object, and it has a child which is the bats flying particle effect. When I use Particle System Play (as suggested) to activate the bats, I must specify the game object which has the particle effect on it. My first attempt was to drag the particle effect, which is a prefab and a child as I mentioned, into the Game Object slot. But when I run the game, it disappears and the bats don't play because it's missing the game object. My second attempt was to create a game object variable, make it visible in the inspector, and drag the bats animation prefab into the slot in the inspector. Then in the FSM I use the variable to identify the bats particle system game object. But when I run the game, this also disappears and the bats don't fly because it's missing the game object.

Do you have any ideas why my object reference keeps disappearing? I've had this happen in other similar situations, and I think it has to do with me not understanding how prefabs work, perhaps. The particle animation is a prefab made by someone who sells premade particle effects on the Unity Asset Store.

Thanks for your help.
Title: Re: Trigger a particle system [Not yet solved]
Post by: Broken Stylus on October 28, 2019, 07:39:54 AM
Because they're references to an instance of a prefab possibly.
More precisely, references between non-prefabs and prefabs don't work well.

Saving the reference as a Global var works around that but is not practical if it's to be used on several objects. I mean, imagine having to save all your references to Game Objects in an endless list of Globals!

Yet, your issue is odd. I know that a prefab will hardly accept references to non-prefab Game Objects, but you're actually encountering a problem that's opposite of that, if I understand it.

Parent; empty object with FSM (non prefab)
¬ Child; the bat-shuriken (prefab)

So you control the particle activation from the parent's FSM, but your Game Object var can't seem to retain the reference to the child at runtime.

I know that you can store a reference to an instance in an Array List. You could use an Array List component (Array Maker extension available on Ecosystem) and then send the "play particle" event to this child by picking it in the array.
There's literally an action for that.

Or you could do something quirkier by having the parent send an event to itself, but set the action to "exclude self" and "send to children". It wouldn't matter here because you have only one child underneath this parent.
Title: Re: Trigger a particle system [Not yet solved]
Post by: 3D Lunatic on October 28, 2019, 09:04:23 AM
Thank you, Broken Stylus. I actually discovered that the particle system was NOT a prefab, sorry about my inexperience. Now that I have made it into a prefab, it stays in the field. The FSM now appears to execute the correct cycle, the only problem is that I'm still not seeing the bats. But at least all the states are being executed.

I'm going to fiddle with it more to find out why I'm not seeing the bats. Your suggestions have got me further along, but I won't yet mark this thread as solved until I figure it out.
Title: Re: Trigger a particle system [SOLVED]
Post by: 3D Lunatic on October 30, 2019, 03:07:44 PM
It works now. There was a script on the particle effect, destroying it after it plays. It was a feature by the FX developer to prevent memory leaks in the game.