- Home /
Rewind particle system (shuriken)
I would like to rewind a particle system. I believe I should be able to do it by setting the time property, as the scripting reference made me believe:
ParticleSystem.time
> Use this to read current playback time or to seek to a new playback time.
I see that we can rewind particle systems in the editor by scrubbing time. However I'm not having any luck with rewinding a particle system in code. Here's what I've been trying it with:
public class RewindParticle : MonoBehaviour {
public float AfterSeconds = 2.0f;
public bool rewinded;
private float rewindStartParticleTime;
private float rewindStartTime;
void Update () {
if (rewinded) {
float rewindToParticleTime = rewindStartParticleTime - (Time.time - rewindStartTime);
if (rewindToParticleTime > 0.0f) {
Debug.Log("rewinding to time " + rewindToParticleTime);
particleSystem.time = rewindToParticleTime;
Debug.Log(particleSystem.time); //this corrently prints what rewindToParticleTime has
}
}
if (particleSystem.time > AfterSeconds) {
if (!rewinded) {
rewinded = true;
Debug.Log("rewinding");
rewindStartTime = Time.time;
rewindStartParticleTime = particleSystem.time;
//these didn't help either
//particleSystem.Stop(); //this stops it for good
//particleSystem.Pause(); //this just pauses it
}
}
}
}
Here's what the output looks like. This starts after 2 seconds. I believe my code is correct:
rewinding
> rewinding to time 1.997295
> 1.997295
> rewinding to time 1.980445
> 1.980445
This goes on until it reaches close to zero. However, the particle system that I'm looking at doesn't care, it just plays as if I'm not setting the time property at all.
The conclusion that I reached so far is that this is a bug and Unity simply ignores setting the time property of ParticleSystem instances. Am I missing something or is this really a bug?
Thank you.
Answer by ricardo_arango · Jan 24, 2013 at 07:18 PM
You need to set the random seed, to make sure you have a deterministic simulation and call ParticleSystem.Simulate (). For example:
using UnityEngine;
public class Rewind : MonoBehaviour {
float time;
public ParticleSystem particleSystem;
void Start (){
particleSystem.randomSeed = 1;
}
void OnGUI (){
time = GUI.HorizontalSlider (new Rect(5,5, 200, 20), time, 0, 10);
time = (int)time; // to animate the simulation in steps
if (GUI.changed)
particleSystem.Simulate (time, true, true)
}
}
Answer by gazialankus · Apr 24, 2012 at 12:38 AM
This is being answered in the forums: http://forum.unity3d.com/threads/133302-Can-t-rewind-a-ParticleSystem.-Bug
Was this ever fixed. I can't find any info about anybody getting simulate to work for either rewinding or for going to time t for the particleSystem.
Answer by Anxo · Sep 15, 2014 at 07:18 PM
Little Late but I needed the particle system to restart too, sadly this is how I had to go about it.
Summery:
particleSystem.Clear(); // if you do not clear, it will keep old particles when you turn the object back on.
particleSystem.gameObject.SetActive(false);
particleSystem.gameObject.SetActive(true);
This is how I use it.
//Particle system is turn to play on awake.
private bool particlesShouldBeOn = false; // this is the bool that controls the particles
void Start(){
StartCoroutine(FlipParticlesOnAndOff());
}
IEnumerator FlipParticlesOnAndOff ()
{
while (true) {
myParticles.Stop (); //Stop the particle system
while (particlesShouldBeOn == false)
yield return null; //wait till bool flips
myParticles.Clear (); // clear any particles that remain.
myParticles.randomSeed = 0; // randomizing seed
halo.SetActive(false); // <--- work around as Stop() does
//not rewind but Awake does....
halo.SetActive(true); // < -- start back up.
while (isCharged)
yield return null; // Wait till we flip the bool off
}
}
Hey, I'm trying to implement this in my project, but not following everything.
Is this coroutine supposed to be running the entire time this game object is alive? and particlesShouldBeOn is used to start/stop the effect?
Line 11 - while what is true? Should that be a variable that is set elsewhere?
isCharged is changed where?
Thanks