Search Unity

How does the Transform's scale work with a particle system?

Discussion in 'Editor & General Support' started by Jessy, Aug 25, 2011.

  1. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,327
    Scale seems to do nothing unless I make it negative, in which case the system disappears. I want to just take some systems and flip them over an axis, so that's not helpful.
     
  2. MrBurns

    MrBurns

    Joined:
    Aug 16, 2011
    Posts:
    378
  3. unmaxim

    unmaxim

    Joined:
    Jul 25, 2013
    Posts:
    3
    Pretty late (3 years passed already :) ), but I had same problem and solved by adding following to my script:

    I have Player GameObject and Particles as child of Player. Player is 2D and I use Scale.x -1 to flip him which makes Particles flipped as well.
    Adding below code to Particles will set localScale.x to -1 which is taken from lossyScale and solve my problem.

    Code (CSharp):
    1.  
    2. public void OnWillRenderObject() {
    3.        if( transform.lossyScale.x < 0 ) {
    4.        transform.localScale = newVector3(transform.localScale.x * -1, transform.localScale.y, transform.localScale.z);
    5.        }
    6. }
    7.  
    tell me if you have better solution, pls
     
    Last edited: Jul 8, 2014
  4. FrankBuss

    FrankBuss

    Joined:
    Dec 22, 2014
    Posts:
    41
    I needed to scale a particle system at runtime, too, and solved it for my case with this script attached to the particle system object:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. public class ScaleParticles : MonoBehaviour {
    6.     void Update () {
    7.         particleSystem.startSize = transform.lossyScale.magnitude;
    8.     }
    9. }
    10.  
    Because of the "[ExecuteInEditMode]" attribute I can place my particle systems and scale them as I like even visually in the editor with the mouse. Works with nested parent transform objects, too, as long as there is no rotation. But might not work with more complex particle systems, I used it only with a simple sprite sheet animation.
     
  5. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    607
    I am trying out this script on a particle system. When you attach the script, are you scaling with the scale widget? Because I am not getting a desired affect. Immediately when I attach the script the particle system changes and looks bad. I would like to add that the particle systems I am using are a prefab that consists of 3 particle emitters and a light. I do not program in C#, so I am not sure if "[ExecuteInEditMode]" is supposed to be a public editor variable or something?
     
  6. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,327
    is googleable, and not C#-specific.
     
  7. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    607
    Got it. Well either way the script is not working with the particle prefab I have. Any idea why the script is immediately changing the way the particle appears without me scaling anything?
     
  8. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    607
    I tested it on some of the standard particle systems and it seems to work pretty well with those so something must be weird with the prefabs I am trying them on :(
     
  9. DigitalBeach

    DigitalBeach

    Joined:
    Jan 17, 2015
    Posts:
    29
    This only works if your initial scale values are 1 and you are scaling them down. We needed to adjust them from the original values.

    Since all the particle settings are instanciated then if you change them they stay changed, we save off a list of all the settings we care about at awake time, then when we change the parent scale, we call an UpdateScale function to set the parameters we cared about. That was startSize,lengthScale, and velocityScale.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. // Handles dynamically scaling of particles attached to objects.
    6. // Goes through all the sub particle systems storing off initial values.
    7. // Function to call when you update the scale.
    8. public class ScaleParticles : MonoBehaviour {
    9.   private List<float> initialSizes = new List<float>();
    10.  
    11.   void Awake() {
    12.     // Save off all the initial scale values at start.
    13.     ParticleSystem[] particles = gameObject.GetComponentsInChildren<ParticleSystem>();
    14.     foreach (ParticleSystem particle in particles) {
    15.       initialSizes.Add(particle.startSize);
    16.       ParticleSystemRenderer renderer = particle.GetComponent<ParticleSystemRenderer>();
    17.       if (renderer) {
    18.         initialSizes.Add(renderer.lengthScale);
    19.         initialSizes.Add(renderer.velocityScale);
    20.       }
    21.     }
    22.   }
    23.  
    24.   public void UpdateScale() {
    25.     // Scale all the particle components based on parent.
    26.     int arrayIndex = 0;
    27.     ParticleSystem[] particles = gameObject.GetComponentsInChildren<ParticleSystem>();
    28.     foreach (ParticleSystem particle in particles) {
    29.       particle.startSize = initialSizes[arrayIndex++] * gameObject.transform.localScale.magnitude;
    30.       ParticleSystemRenderer renderer = particle.GetComponent<ParticleSystemRenderer>();
    31.       if (renderer) {
    32.         renderer.lengthScale = initialSizes[arrayIndex++] *
    33.             gameObject.transform.localScale.magnitude;
    34.         renderer.velocityScale = initialSizes[arrayIndex++] *
    35.             gameObject.transform.localScale.magnitude;
    36.       }
    37.     }
    38.   }
    39.  
    40. }
     
  10. jmasinteATI

    jmasinteATI

    Joined:
    Jul 14, 2015
    Posts:
    7
    I had a similar situation with fireworks. I had a celebration sequence and wanted to add fireworks. The fireworks were far too large for the scene and it was cumbersome to change all of the values to make it fit. To remedy it, I placed my fireworks game object into a layer called fireworks. Then I created a camera just for the fireworks, which only renders the fireworks layer. I set the depth to 1 so that they would appear over the rest of the scene. My fireworks camera's distance from the fireworks determined their scale in the final game.
     
  11. NinjaiAssassin

    NinjaiAssassin

    Joined:
    Dec 30, 2014
    Posts:
    1
    I'm using Unity 5.5.2f1 Personal. Wanted to post current answer in case people were still having trouble with scaling a group of particles using the transform on the parent object. It's 2017 and even now I couldn't find the answer on forums. But here's the solution without having to use any additional plugins or code:

    The default of a particle system is set to Local and does not allow you to scale it using the transform. I don't know how long this has been a part of Unity, but when using the Particle System, look for the tab labeled Scaling Mode. The default is marked as Local. If you have a group of particles together for an effect, the transform on the parent won't scale everything correctly. Change the Scaling Mode on ALL of your particle systems to Hierarchy. This will allow you to scale the entire particle system from the parent's transform. Hope this helps!

    Screenshot.jpg
     
  12. NathanRahl

    NathanRahl

    Joined:
    Nov 17, 2013
    Posts:
    1
    You sir are my hero for the day!
     
  13. Farelle

    Farelle

    Joined:
    Feb 20, 2015
    Posts:
    504
    let me just throw this in here: THANK YOU! :D
     
  14. zxgats1986

    zxgats1986

    Joined:
    May 23, 2017
    Posts:
    2
    It's make sense in Unity 5.5.3, but when I upgrade to Unity 2017.2.0f3, It dosen‘t work.Why?
     
  15. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    95
    Yeah, it isn't working for me either. Have you found a solution?
     
  16. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,445

    Thanks,
    this works great!
     
    Alverik likes this.
  17. LucidZach

    LucidZach

    Joined:
    Dec 20, 2017
    Posts:
    5
    Maybe a bug, worked for me in 2017.1.1f1
     
  18. sysmaya

    sysmaya

    Joined:
    Nov 30, 2013
    Posts:
    14
    ROSSOMLAND 7 Years later. Very Thanks
    Scaling Mode : Hierarchy
     
  19. nntgam

    nntgam

    Joined:
    Mar 9, 2019
    Posts:
    2
    You are my idol :D
     
  20. GoblinGameWerks

    GoblinGameWerks

    Joined:
    Sep 14, 2019
    Posts:
    1

    This works like a charm.... date today is 9/13/19 (I have the latest unity version to date)
    My man, you are gentleman an a scholar.