need help triggering particles while in dialogue

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
billy
Posts: 2
Joined: Mon Jul 11, 2016 7:42 pm

need help triggering particles while in dialogue

Post by billy »

hi everyone im billy.

i need help triggering one-shot particle effects in unity while using dialogue. i have the particle systems i want but i need help triggering them during dialogue. from what i understand particles can be triggered using colliders, but if im talking to an NPC i dont want to be moving around so what do i do?? ive been going over the manual for days and trying to do it myself but im very new to scripting so it hasnt stuck with me at all. please help thank you
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: need help triggering particles while in dialogue

Post by Tony Li »

Hi Billy,

Here's a way to do it without colliders or scripts:

1. Create a particle system with these parameters:
  • Duration: The duration you want the particle effect to last.
  • Looping: Unticked.
  • Play On Awake: Ticked.
For this example, let's say you named the particle system GameObject "MyParticleEffect".

2. Make MyParticleEffect a child of an active GameObject. For example, create an empty GameObject named "ParticleSystems" positioned at (0,0,0). Then make MyParticleEffect a child of ParticleSystems. Deactivate MyParticleEffect, but leave ParticleSystems active.

3. To one-shot the particle effect during dialogue, add this to the dialogue entry node's Sequence field:

Code: Select all

SetActive(MyParticleEffect)
This will be a true one-shot effect, as in it will only run once in the scene. If you need it to be able to re-run every time the conversation hits this dialogue entry node (for example, if the player revisits the conversation), let me know; I'll describe how to do that.
billy
Posts: 2
Joined: Mon Jul 11, 2016 7:42 pm

Re: need help triggering particles while in dialogue

Post by billy »

thank you so so much that worked perfectly. and for my purposes i won't be re-running the same conversation, but im interested in knowing how to trigger a particle if i wanted it to loop? like the same thing you did with the one-shot, but having it loop until the next section of dialogue?
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: need help triggering particles while in dialogue

Post by Tony Li »

The heavy-handed way is to use the SetActive() command again to deactivate the GameObject:

Code: Select all

SetActive(MyParticleEffect,false)
However, the more graceful way is to write a custom sequencer command. You can create a C# script file named SequencerCommandParticleSystem containing this code:

Code: Select all

using UnityEngine;

namespace PixelCrushers.DialogueSystem.SequencerCommands {

    public class SequencerCommandParticleSystem : SequencerCommand {

        public void Start() {
            var subject = GetSubject(0);
            var command = GetParameter(1);
            var particleSystem = (subject != null) ? subject.GetComponent<ParticleSystem>() : null;
            if (particleSystem == null) {
                if (DialogueDebug.LogWarnings) Debug.LogWarning("Dialogue System: Sequencer: ParticleSystem(" + GetParameters() + "): Can't find a ParticleSystem on " + GetParameter(0));
            } else {
                if (DialogueDebug.LogInfo) Debug.Log("Dialogue System: Sequencer: ParticleSystem(" + particleSystem + ", " + command + ")", particleSystem);
                if (string.Equals(command, "play", System.StringComparison.OrdinalIgnoreCase)) {
                    particleSystem.Play();
                } else if (string.Equals(command, "pause", System.StringComparison.OrdinalIgnoreCase)) {
                    particleSystem.Pause();
                } else if (string.Equals(command, "stop", System.StringComparison.OrdinalIgnoreCase)) {
                    particleSystem.Stop();
                } else {
                    if (DialogueDebug.LogWarnings) Debug.LogWarning("Dialogue System: Sequencer: ParticleSystem(" + GetParameters() + "): Unrecognized command " + GetParameter(1));
                }
                Stop();
            }
        }
    }
}
This will add a sequencer command ParticleSystem() that takes two parameters. The first is the GameObject name, same as the SetActive() command you used. The second is "play", "pause", or "stop" (without quotes). For example, this sequence will start the particle effect at the 1-second mark and then stop it gracefully (allowing the current particles to fade out naturally) at the 3-second mark:

Code: Select all

ParticleSystem(MyParticleEffect,play)@1;
required ParticleSystem(MyParticleEffect,stop)@3
I added the "required" keyword to guarantee that the Dialogue System runs the stop command even if the player cancels the dialogue entry early.
Post Reply