Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

How to scale particle system?

Discussion in 'Scripting' started by leegod, Jun 14, 2017.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,144
    So I want to scale down generated (from prefab) particle effect, but how?

    I found this thread by googling,

    http://answers.unity3d.com/questions/621691/scale-shuriken-particle-system-with-parent-transfo.html

    https://docs.unity3d.com/ScriptReference/30_search.html?q=ParticleSystemScalingMode

    so I tried like below,

    Code (CSharp):
    1. public void SetScale()
    2.     {
    3.         ParticleSystem.MainModule[] sys = GetComponentsInChildren<ParticleSystem.MainModule>();
    4.         for (int i = 0; i < sys.Length; i++)
    5.         {
    6.             sys[i].scalingMode = ParticleSystemScalingMode.Hierarchy;
    7.         }
    8.         transform.localScale *= BattleStatus.Instance.FXScaleVal; (this value is like 0.3)
    9.     }
    but this does not scale down at all.

    Why and how to then?

    Why you made this so hard to control?
     
    BrainSlugs83 likes this.
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,492
    transforming the scale of the particle system by itself won't do anything and I think it's to do with the other scales within the particle system itself. So what you have to decide is, do you want to change the scale of the particles? Or do you want to change the emission shape/angle/radius?

    If you want to change both at the same time then you'd have to change the angle and particle size all at the same time and then that should have the effect you want. While this may be tedious at first it does make sense because it allows you to have a lot more control over the individual bits of your particle system.

    https://docs.unity3d.com/ScriptReference/ParticleSystem.Particle.html

    I'll see if I can dig up some proper code for you in the documentation, should be there somewhere.

    Edit: https://docs.unity3d.com/ScriptReference/ParticleSystem-shape.html

    Found one for shape, will look for others.

    Edit 2: Oh! I may have found a solution to your specific problem, someone asked about this a long while back, looks like I was wrong and you can scale the particle system generally just fine using code, my bad, haven't tested this but it looks complete. This is why we research this type of thing carefully, looks like there is in fact two ways of doing it, sorry about the mixup everyone.

    https://forum.unity3d.com/threads/how-does-the-transforms-scale-work-with-a-particle-system.101964/
     
    Last edited: Jun 14, 2017
    mysunnytime likes this.
  3. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,144
    so basically my original post's ParticleSystemScalingMode.Hierarchy; works.

    But the mode change by script result error, so I manually changed all prefab effect, and then at its parent transform,

    Code (CSharp):
    1. public void SetScale()
    2.     {
    3.         transform.localScale *= BattleStatus.Instance.FXScaleVal;  // (0.3 or anything you want value)
    4.      }
    call this function right after effect generated by instantiate.
     
    BrainSlugs83 likes this.
  4. Rodrigodpl

    Rodrigodpl

    Joined:
    May 17, 2018
    Posts:
    1
    I found a solution by setting the scaling mode in the particle system to hierarchy then scaling the gameobject attached to.
     
  5. afshin_a_1

    afshin_a_1

    Joined:
    Apr 26, 2015
    Posts:
    33
    the right solution for this is:
    look for "scaling mode" on your particle system. then
    A- if you have a single particle system, set it to "Local".
    B- if you have a parent game object and other particle systems are children of it(multiple particle system), set it to "Hierarchy" for all of children.
    then scale down the transform, just as you do with regular game object.
     
    Last edited: Apr 6, 2020
  6. Cactus_on_Fire

    Cactus_on_Fire

    Joined:
    Aug 12, 2014
    Posts:
    638
    Thanks!
     
    akrelle_unity likes this.
  7. BrainSlugs83

    BrainSlugs83

    Joined:
    Jun 18, 2015
    Posts:
    36
    This worked for me in Unity 2019.4.9f1.

    Code (CSharp):
    1. float scale = 0.3f;
    2. var psys = GetComponentsInChildren<ParticleSystem>();
    3. foreach (var ps in psys)
    4. {
    5.     var main = ps.main;
    6.     main.scalingMode = ParticleSystemScalingMode.Local;
    7.     ps.transform.localScale = new Vector3(scale, scale, scale);
    8. }
     
    SamwiseStevens likes this.
  8. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    141
    There's a slight misinformation in your solution.

    Here's the definition and effect of each option in the "Scaling Mode":
    "Local" - The particle system will only be affected by its own (its game object) scaling. Any scaling of its parent will not affect it in any way.
    "Hierarchy" - The particle system will scale based on its parent and its own transform. The scaling will affect both the shapes and generated particles' sizes.
    "Shape" - The particle system will scale based on its parent and its own transform. The scaling from the parent and its own transform will affect the shape's size, but not the particles sizes themselves.

    Hierarchy and Shape have a super tiny mini impact on performance. (It's super minimal, but it's there if you ever have dozen thousands particles systems scaled at once) which scales a bit up if you affect the particle system with un-ruled physics like collision. (When particles, in a particle system, get affected by outside forces like with a collision, they get un-ruled and become instances until their death.)

    Shape is really useful if you want to change the size of a particle's emission shape from its parent' origin's point.
    The best example of a concept that uses it that comes to my mind would be with bubbles in a liquid. Let's say that you simulate the emptying of a bottle filled with sparkling water. You could reduce the scale of the liquid on Y and then change shape's sizes manually in a script or you could just setup the Scaling Mode to "Shape" and animate the scaling of the liquid.

    Another example is if you were to simulate animals as particles like fishes or birds and want them to fly or swim in a bigger space. Setting the Scaling Mode to "Shape" will grow the size of the emitter and forcefield, but will keep the particles (like the birds and fishes) at their original sizes.

    One detail to remember is that scaling the particle system while in "Shape" will not move the existing particles while scaling it while in Hierarchy or Local will move the particles in the resizing. If you resize a pool with fishes while in Shape mode, the fishes will remains, during the resizing, at their original position (and sizes) while the other modes (if done properly) will move the fishes at the point proportional to their original position in size.
     
  9. ArturDenislamov

    ArturDenislamov

    Joined:
    Nov 16, 2020
    Posts:
    3
    Thank you very much, a helpful explanation!
     
unityunity