Search Unity

Correct way to place particle emissions at contact point? EmitParams.position bugged?

Discussion in 'General Graphics' started by SoftwareGeezers, Dec 15, 2016.

  1. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Trying to spawn a particle effect on impact. I have a particle system on my ship operating in world space and call a script to spawn particles on a 2D collision.

    How am I supposed to position the particles? I was thinking system.emit(emissionparams.position) but that's overriding the particle system. Do I literally just move the particle emitter's transform to the contact point and then emit?

    Also, do you have to manually reference secondary particle systems (child objects) to emit or is there a way to combine the two in a single call, so when the first fires it also triggers the second?
     
    Last edited: Dec 15, 2016
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    You can call Emit with an override to give the particles a new position https://docs.unity3d.com/ScriptReference/ParticleSystem.EmitParams.html
    This will only override it for this emit call, so its the right thing for what you need.
    You could also spawn a new particle system at that location, if its a collision point then this is also fine.
    You could also just grab the particle with GetParticles and manually set the positions.

    If they are set as subemitters then they will work with the Emit call and wont need any additional calls.
     
  3. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Cool. This leads on to a better idea than I was doing for common effects like collision sparks which is to have one universal Shuriken system at the root level and call Emit(pos) with the collision (or whatever) position. Saves a bit of object creation and cleanup versus per-object effects.
     
  4. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Hang on, that's what I was doing in my OP and it isn't working. No particles appear when I supply a position to the emitparams.

    Code (CSharp):
    1.     public ParticleSystem FX_sparks;
    2.     ParticleSystem.EmissionModule FX_em_sparks;
    3.     ParticleSystem.EmitParams FX_params_sparks;
    4.  
    5.     void Awake(){
    6.         instance = this;
    7.         FX_em_sparks = FX_sparks.emission;
    8.         FX_params_sparks = new         ParticleSystem.EmitParams();
    9.     }
    10.  
    11.     public void Sparks(Vector2 pos, int count){
    12.         print("spark at "+pos+"   "+count);
    13.         FX_params_sparks.position = Vector3.zero;
    14.         FX_sparks.Emit(FX_params_sparks, count);
    15.     }
    If I change FX_params_sparks.position, no particles are emitted. If I comment out that line, particles are emitted. If I use other params like size, they work so long as I don't change position. That's spawning particles at (0,0,0) - if I print out FX_params_sparks.position without changing it, it's (0,0,0).
     
  5. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Working from the manual example, this code spawns a red particle upwards with no variation of trajectory. It seems all the properties for direction are zero'd when position is changed.

    Image1.jpg

    Create a new scene. Add a blank GO. Add this script. Run...
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. // In this example, we have a particle system emitting green particles; we then emit and override some properties every 2 seconds.
    4. public class EmitExample : MonoBehaviour
    5. {
    6.     public ParticleSystem system;
    7.  
    8.     void Start()
    9.     {
    10. //        // A simple particle material with no texture.
    11. //        Material particleMaterial = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
    12.  
    13.         // Create a green particle system.
    14.         var go = new GameObject("Particle System");
    15.         go.transform.Rotate(-90, 0, 0); // Rotate so the system emits upwards.
    16.         system = go.AddComponent<ParticleSystem>();
    17. //        go.GetComponent<ParticleSystemRenderer>().material = particleMaterial;
    18.  
    19.         // Every 2 secs we will emit.
    20.         InvokeRepeating("DoEmit", 2.0f, 2.0f);
    21.     }
    22.  
    23.     void DoEmit()
    24.     {
    25.         // Any parameters we assign in emitParams will override the current system's when we call Emit.
    26.         // Here we will override the start color and size.
    27.         var emitParams = new ParticleSystem.EmitParams();
    28.         emitParams.position = Vector3.zero;
    29.         emitParams.startColor = Color.red;
    30.         emitParams.startSize = 0.2f;
    31.         system.Emit(emitParams, 10);
    32.         system.Play(); // Continue normal emissions
    33.     }
    34. }
    The default particle system sprays a cone of white particles upwards. Every two seconds, 10 overlapping particles are emitted that travel straight up from centre. Comment out the line "emitParams.position = Vector3.zero;" and 10 red particles are sprayed in the cone according to the system emission shape.

    Bug?
     
    Last edited: Dec 16, 2016
  6. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
  7. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Fixed. Thanks! I see this is actually mentioned on the params.position page... :oops:Although to be fair on myself, the particles weren't appearing so I wasn't aware they were losing their shape and projection.
     
    richardkettlewell likes this.
  8. AlcoleponeHome

    AlcoleponeHome

    Joined:
    Jun 9, 2018
    Posts:
    47
    Is there anyway to pass on the rotation of the emission shape. i'm using a similar system as above, but am emitting particles in cone shape. but i want to pass on a rotation for the cone along with a position....is this possible?
     
  9. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    It's possible by modifying the shape module transform before calling Emit.. something like:

    Code (CSharp):
    1. var shape = ps.shape;
    2. shape.rotation = myRotation;
    3. ps.Emit(1, myEmitParams);
    you may want to restore the old rotation afterwards, depending on what you're doing :)
     
  10. AlcoleponeHome

    AlcoleponeHome

    Joined:
    Jun 9, 2018
    Posts:
    47
    thats the method i went for currently.
    There is a issue though. As there are mutlple gameobjects with the script that calls the particle system "emit", some particles come out at the wrong rotation. Its as if all of the emit commands are sent in one go, so if 2 script call the emit function and rotate the system, both emit particles face the same direction, ignoring one of the rotations..
     
  11. AlcoleponeHome

    AlcoleponeHome

    Joined:
    Jun 9, 2018
    Posts:
    47
    I'm trying to use the emit function as a means of have a single aprticle system for bullet impacts, and other simialr repeating spot vfx. Is there a better method for doing this currently?
     
  12. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    That shouldn't happen. If you can prove it in a repro project, report it as a bug and I'll see what's going on. You can link to this forum thread in your bug report, and reply here with the case number, to help connect the dots :)

    That's the best way, yeah.
     
    karl_jones likes this.
  13. helarts

    helarts

    Joined:
    Jun 12, 2013
    Posts:
    6
    It surely happens (and other odd things) in EditMode if the ParticleSystem.Emit function is called in LateUpdate.
    Here is a very simple test case, just moving one of the spheres in the attached scene should show the issue.
     

    Attached Files:

  14. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    To be clear, report it as a bug means this: https://unity3d.com/unity/qa/bug-reporting
     
    karl_jones likes this.