Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Problems with particles: created but not moving (and ignoring emitter shape too!)

Discussion in 'Physics' started by mteom, Feb 6, 2015.

  1. mteom

    mteom

    Joined:
    Aug 6, 2014
    Posts:
    7
    Hello.
    I hope this is the right place for this issue.

    I recently started playing with the particle system (2d project).
    I can create them just fine, but when i instantiate the object that has a particle system attached to it, i notice very strange behaviours.
    First, i have my object in the "Resources" folder as prefabs, and they get instantiated in an OnCollisionEnter2d() method.
    If i test the prefab in the editor (by dragging it into the scene then press the simulate button near the emitter)everything works just fine, the same if i test the game by pressing play, then dragging the prefab into the scene in realtime and repeating the above; if i test the scene by playing it, without creating instances myself, but only through the instantiating script, the particles get created but they don't move at all, they just appear, change size then vanish.
    All this happen if the emitter shape is set to "box"; if i change it to any other shape, the particles moves normally.....but they seems to ignore the shape of the emitter!
    So if i set the emitter to hemisphere, i would expect the particles to only go upwards and a little sideways...instead they all act like it's a sphere emitter.
    This only if i instantiate them through scripting, if i drag and drop all works fine.

    This is how the particles are created:
    Code (CSharp):
    1. public class ParticleHelper : MonoBehaviour
    2. {
    3.     // Singleton
    4.     public static ParticleHelper Instance;
    5.     //
    6.  
    7.     public ParticleSystem HeartEmitter;
    8.     public ParticleSystem CoinEmitter;
    9.     public ParticleSystem StarBurst;
    10.  
    11.     void Awake()
    12.     {
    13.         // Register the singleton
    14.         if (Instance != null)
    15.         {
    16.             Debug.LogError("Multiple instances of ParticleHelper!");
    17.         }
    18.         Instance = this;
    19.     }
    20.  
    21.     // Create an explosion at the given location
    22.     public void HeartParticles(Vector3 position)
    23.     {
    24.         instantiate(HeartEmitter, position);
    25.     }
    26.  
    27.     public void CoinParticles(Vector3 position)
    28.     {
    29.         instantiate(CoinEmitter, position);
    30.     }
    31.  
    32.     public void StarParticles(Vector3 position)
    33.     {
    34.         instantiate(StarBurst, position);
    35.     }
    36.  
    37.  
    38.     // Instantiate a Particle system from prefab
    39.     private ParticleSystem instantiate(ParticleSystem prefab, Vector3 position)
    40.     {
    41.         ParticleSystem newParticleSystem = Instantiate(prefab, position, Quaternion.identity) as ParticleSystem;
    42.         // Make sure it will be destroyed
    43.         Destroy(newParticleSystem.gameObject, newParticleSystem.startLifetime);
    44.         return newParticleSystem;
    45.     }
    46.  
    47. }
    Then the object is instantiated in another script like this:
    Code (CSharp):
    1.     void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.gameObject.name == "kiwi")
    4.         {
    5.             ParticleHelper.Instance.HeartParticles(transform.position);
    6.             ApplicationManager.myScore += points;
    7.             playerScript.setPower();
    8.             Destroy(this.gameObject);       //the gameobject associated to this script (the gems) gets destroyed
    9.         }
    10.     }
    11.  
    I don't think I am doing anything wrong, also the settings are correct since if they weren't the dragged reference of the object wouldn't play the particles correctly.
    Is this a sort of bug or i am doing something wrong?
    Any help is appreciated.
    Thank you.
     
  2. mteom

    mteom

    Joined:
    Aug 6, 2014
    Posts:
    7
    I forgot to say that the game object that has the helper script has 3 reference variables which i have dragged the game objects particles into; also that game object is set to "DontDestroyOnLoad".
    I tried to not using the helper script but instead use(into the object that receive collisions) resourcesLoad, but i have the same result.
    Only direct dragging seems to work.
     
  3. mteom

    mteom

    Joined:
    Aug 6, 2014
    Posts:
    7
    Tested, in the same situation, every other emitter shape, they all get ignored and act like is a sphere, while box only works if "random direction" is ticked, but then it acts like a sphere again!
    I will try to make a new scene and scripts from scratch and see how it goes..
     
  4. mteom

    mteom

    Joined:
    Aug 6, 2014
    Posts:
    7
    Nothing...brand new scene, brand new scripts, new objects and everything....
    Still acting as above.
    Makes me think there is something wrong in how the particle object is instantiated.
     
  5. mteom

    mteom

    Joined:
    Aug 6, 2014
    Posts:
    7
    Found what is happening.
    The instantiated object is rotated to 0 degrees instead of 270 like the prefab.
    Does anyone know why this is happening?
    I would have thought that if i instantiate an object it will make a perfect clone of the original...
     
  6. mteom

    mteom

    Joined:
    Aug 6, 2014
    Posts:
    7
    Actually, i retraced all my steps and if i create a prefab that has for rotation -90, i then instantiate it with quaternion.identity (which should retain the original rotation) the actual rotation is changed to 270, so the particles seems that they are not moving because the angle is wrong!
     
  7. mteom

    mteom

    Joined:
    Aug 6, 2014
    Posts:
    7
    My fault.
    Didn't read the docs very well and thought that quaternion.identity was for having no rotation, as in retaining the original from the prefab.
    Now i instantiate with the quaternion.euler() command and everything works perfectly.