Returning particle system values back to orginal

Hi all,
I have another question .
So i made a code that in collision in lowers health and change particle system values, but i wanna make now when its not in collision is always in original state ? original values of the prefab ?
So basically when not in collision it values of particle system will go slowly with Time.deltatime slowly up to the original values and stay until it gets into collision again
So here is my code

function OnParticleCollision (other : GameObject) {
     if(other.tag =="ball")
     {
            var allChildren = gameObject.GetComponentsInChildren(ParticleSystem);
            for (var child : ParticleSystem in allChildren) 
                {
                if(Health > 30 &&Health <= 70)
                    {
                        child.startSize -= 1 * Time.deltaTime;
                    }
                if(Health > 70)
                    {
                        child.startSize -= 3 * Time.deltaTime;
                    }
                }
     }
 }

But how to i make on collision exit or when its not in collision with ball the particles will go back to original state slowly.
I tried researching on google but idk how to put it in short words. I know i saw once somewhere when someone used some code to save values or particle like safe prefab values or something and then return it to original state slowly with time?
I would really appreciate if someone could help

Just store a copy of you original particle system in awake or start and use that to reset your adjusted particle system.

import System.Collections.Generic;

var PS : ParticleSystem[];
var cachedPS : List.<ParticleSystem> = new List.<ParticleSystem>();

function Start()
{
    PS = GetComponentsInChildren<ParticleSystem>();
  
    for(var i=0 : int; i<PS.Lenght; i++)
    {
        ParticleSystem ps = PS[i];
        chachedPS.Add(child);
    }
}

function Update()
{
    //... do stuff to PS
    //... when wanting original settings
    for(var i=0 : int; i<PS.Lenght; i++)
    {
        PS[i] = chachedPS[i];
    }

}
1 Like

Ok thanks for reply, but how do i save it for all childrens in parent ?
as i did

 var allChildren = gameObject.GetComponentsInChildren(ParticleSystem);
            for (var child : ParticleSystem in allChildren)

it gets component particle system for all childrens in parent but how do i save these all particle system components from all childrens in parent :?

i changed my previous answer to your requirements…

Ok thanks so much for help
Im getting 3 errors

    for(int i=0; i < PS.Lenght; i++)
    {
        ParticleSystem ps = PS[i];
        chachedPS.Add(child);
    }

11,13): BCE0044: expecting ;, found ‘i’.
11,36): BCE0043: Unexpected token: ).
13,23): UCE0001: ‘;’ expected. Insert a semicolon at the end.

ah yes i see switch to c# there :slight_smile: will change it to js
also could be lenght is not with a capital L… i don’t normally use js

Thanks it worked !
i have one more questions
I have this script

function OnParticleCollision (other : GameObject) {
     var allChildren = gameObject.GetComponentsInChildren(ParticleSystem);
            for (var child : ParticleSystem in allChildren) {
     if(other.tag =="ball" && sm)
     {
         var v = GetComponent(ParticleSystem);
        v.enableEmission = true;
     }
     else
     {
     var b = GetComponent(ParticleSystem);
        b.enableEmission = false;
     }
}
}

But it looks that it just sets emission to true but doesnt sets to false ?
How can i make so when its not with collision with particle it disable emmision [of smoke]
or more like if its not colliding with anything it sets to false, or original is false but ONLY when in collision with ‘ball’ it sets to true otherwise its false ?
Thanks

anyone could help on this question above :?

child.enableEmission instead of var v = GetComponent…

but i dont want to enable child particles, i have particle ssytem on parent which i want to enable whenever the ball particle collide with the parent and when they are not colliding then disable/false emission

then why do you get all the children’s ParticleSystem?
so let me get this straight, you have a ball and when it collides with one of the particles of a certain particle system you want to enable that particle systems emitter for “one play” right?
oh and the ball is also a particle, from another particle system?

So basically the ‘ball’ is a particle system with collision
the ‘ball’ collides with the particle system im working on that has childs which are all particle systems and it takes away health, ok i got it [its in another script the ball which takes away health]
But when the ball particle hits the particle system im working on i want it to emit extra particle system which is attached to the parent
Or is there any other way i could play particle system when ball collides with this parent of child lets say its ‘wall’ so is there any other way i can play another particle ‘hit particle’ when the ball collides with wall [they are all particle system]
But the most important is to stop playing this hit particle when its not in collision. or when not in collision stop that hit particle emission
[I made when it it hits it plays start emission on this particle system hit but i cant figure out how to stop it when collision is false, because on other function like trigger exit or collision stay, enter exit, but i didnt found any function as particleoncollisionexit i think there isnt such function so basically im looking how to make this function so when not in collision with ball the emission of hit particle is false.

do you have looping off?
if looping is off you can check if there are any particles still alive or need to be played with ParticleSystem.IsAlive(true);

Hi i looked through google and couldnt find anything tried to play around but nothing good :confused: gonna go sleep now
the looping on the particle is on, basically i just want it to play when in collision with ball, else dont play or if its not colliding with ball then stop playing it.

something like this?

var ps : ParticleSystem;

functionStart()
{
    ps = GetComponent(ParticleSystem);
}

functionOnParticleCollision (other : GameObject)
 {

    if(other.tag =="ball")
    {
        ps.loop = false;
        ps.Play();
    }
}
1 Like

Thanks much it worked but idk why when its collision and plays particle some of the child particle stops playing but i will try to figure out by my self now thank you