Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Particle System not starting immediately

Discussion in 'Scripting' started by chazgw, Mar 15, 2021.

  1. chazgw

    chazgw

    Joined:
    Dec 19, 2018
    Posts:
    16
    I'm looking to get my particle system to start immediately upon Play(). I have the Start Delay set to 0, but there's a delay - I've set its Emission->Rate Over Time as 8 (for an automatic gun), but it doesn't start until the the first interval. In this case, the first particle emits at 0, plus (I'm guessing here) one second divided by the number of shots per second, so the first particle is visible at roughly .125 seconds from the time its Play() method is called.

    So, how do I fix this to start immediately?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
  3. chazgw

    chazgw

    Joined:
    Dec 19, 2018
    Posts:
    16
    That bumps it up to at least 21 particles immediately, which isn't my desired effect for a gun. It also has them starting away from the source. By using prewarm, you'd think it might generate the first 8 immediately and then work the way it was intended, so I don't know why the number is so high.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    If you're going for poofs of smoke as rounds are fired, you might want to set its emission to zero and then call the .Emit() method on it periodically to poof out some smoke. That should be instantaneous.
     
  5. chazgw

    chazgw

    Joined:
    Dec 19, 2018
    Posts:
    16
    T
    Thank you, it looks like it was as simple as that - calling Emit() with one particle, followed by Play().

    upload_2021-3-15_22-21-1.png
     

    Attached Files:

    StellarVeil, hellobody and mattking99 like this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    Sweet! I think the .Play() call is not needed, as long as the ParticleSystem is already playing.

    Reason: I have emitted a particle or two in my career. :)
     
    hellobody and chazgw like this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    I actually called it with quite a few particles and made them very translucent for this effect:

     

    Attached Files:

    chazgw likes this.
  8. LostLight1

    LostLight1

    Joined:
    Dec 13, 2019
    Posts:
    3
    This worked great for me, I wish they would have this as part of the control but maybe this has something to do with making your own systems. However this did work on Asset Store system's that I didn't create. So

    private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position)
    {

    //https://forum.unity.com/threads/particle-system-not-starting-immediately.1075693/
    ParticleSystem newParticleSystem = Instantiate(prefab,position,Quaternion.identity) as ParticleSystem;
    newParticleSystem.Emit(1);
    /// -----------------------------
    // Make sure it will be destroyed
    /// -----------------------------
    /// https://stackoverflow.com/questions/41393814/unity-5-5-obsolete-particle-system-code
    Destroy(
    newParticleSystem.gameObject,
    newParticleSystem.main.duration
    );

    return newParticleSystem;
    }

    Thanks to Creepy Cat
    3D Games Effects Pack Free
    3D Games Effects Pack Free | VFX Particles | Unity Asset Store
    for the starter script..

    Also Added Sound and the time the system is done.

    Full Edited

    using UnityEngine;

    /// ------------------------------
    /// Creating instance of particles
    /// ------------------------------
    ///
    [RequireComponent(typeof(AudioSource))]
    public class InstanceExample : MonoBehaviour
    {
    /// ------------------------------
    /// Singleton
    /// ------------------------------
    public static InstanceExample Instance;
    public ParticleSystem effectA;
    public ParticleSystem effectB;
    private AudioSource Audio = null;

    void Awake()
    {
    /// ---------------------
    // Register the singleton
    /// ---------------------
    if (Instance != null)
    {
    Debug.LogError("Multiple instances of InstanceExample script!");
    }

    Instance = this;

    Audio = gameObject.GetComponent<AudioSource>();
    Audio.playOnAwake = false;
    /// https://stackoverflow.com/questions/41393814/unity-5-5-obsolete-particle-system-code
    ParticleSystem.MainModule main = effectB.main;
    //Stops Loops if there is one.
    main.loop = false;

    }

    void Update(){

    if (Input.GetKeyDown(KeyCode.Space))
    {
    /// -----------------------------------------
    /// Instanciate into a box of 5 x 5 x 5 (xyz)
    /// -----------------------------------------
    InstanceExample.Instance.Explosion(new Vector3(Random.Range(-5.0f, 5.0f), Random.Range(-5.0f, 5.0f), Random.Range(-5.0f, 5.0f)));

    if (Audio.clip != null)
    {
    Audio.Play();
    }
    }

    }

    /// -----------------------------------------
    /// Create an explosion at the given location
    /// -----------------------------------------
    public void Explosion(Vector3 position)
    {
    instantiate(effectA, position);
    instantiate(effectB, position);
    }

    /// -----------------------------------------
    /// Instantiate a Particle system from prefab
    /// -----------------------------------------
    private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position)
    {

    //https://forum.unity.com/threads/particle-system-not-starting-immediately.1075693/
    ParticleSystem newParticleSystem = Instantiate(prefab,position,Quaternion.identity) as ParticleSystem;
    newParticleSystem.Emit(1);
    /// -----------------------------
    // Make sure it will be destroyed
    /// -----------------------------
    Destroy(
    newParticleSystem.gameObject,
    newParticleSystem.main.duration
    );

    return newParticleSystem;
    }

    }
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    Thanks for posting this, but it would work even greater for everybody else if you went back and formatted your code properly so it isn't a wall of incomprehensible text barf for the future to suffer.

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  10. mxjnordgren

    mxjnordgren

    Joined:
    Jul 30, 2019
    Posts:
    7
    For anyone else coming across this, I was able to solve the issue by using a single count "Burst" emission rather than "Rate over Time" in the Emission module.
     
    Filip8429, CheMBurN, Oni07R and 2 others like this.
  11. MiahTRT

    MiahTRT

    Joined:
    Jun 21, 2019
    Posts:
    1
    This worked for me, thank you!
     
  12. StellarVeil

    StellarVeil

    Joined:
    Aug 31, 2018
    Posts:
    73
    Thank you :)
     
  13. CACTUU_games

    CACTUU_games

    Joined:
    Mar 23, 2021
    Posts:
    1
    Thank you men!I couldn't decide that problem in my game for a long time.But now thanks to you i can to do this!The Emit() method is similar to the WakeUp() from Rigidbody