Trying to time multiple particles with animation
Hi all,
We have a 3D animation that we need to time particles to. We have 2 separate sets of particles we need to time. One set that happens at the 5 second mark of the animation and lasts for 2 seconds and one that happens at the 8 second mark of the animation and lasts for 3 seconds. And we want the animation to loop with it's particles. We're finding it impossible to do. We're currently trying to achieve this using the delay on the Tween function. The problem is that the delay function also sets the duration of the particle.
Any help would be GREATLY appreciated.
Hi Ian,
This sounds cool! You can do custom particle timing by passing in a particle time to the particle’s material’s main pass:
To do this, lets first create a script with a reference to our particle’s material. In Objects panel, press Add New > Empty Object. Then in the Inspector panel, click Add Component > Script. Set this script to Initialized. Then in your resources panel, let’s modify this script. Add the following to get a reference to your particle’s material.
Keeping track of when animation has started
Then we need to keep track of when our animation was called. Right below that code, we initialize a variable for us to put this information into.
When we play an animation, we will set to this to be our currentTime (getTime())
Then in our update loop, we pass in our particle time information when it’s within some seconds from when the lens has started.
Playing your Particles
To help with this, we can create a dictionary that contains our particle timing information:
The name comes from our reference above.
To create our update loop we will create an event like this:
In that update loop we’ll start by keeping track of how many seconds have elapsed:
Then, we’ll go through each item in particleTiming. To make it easier, we’ll store our information in a shorter variable.
for (var i = 0; i < particleTiming.length; i++) {
var particleName = particleTiming[i].name;
var startTime = particleTiming[i].start;
var lastForTime = particleTiming[i].lastFor;
// do something here
}
To figure out when our particle should play we’ll subtract when we want the animation to start from the time elapsed since animation started
If our particleTime is greater than the time we want our particles to last, we can just set the time to a negative number (meaning simulation will not occur).
if (particleTime > lastForTime) {particleTime = -1;
}
Finally, we will pass this time into our particle so it can be rendered.
Final Script
and you call
where you play your animation.
Can't wait to see what you come up with! Don't forget to share :)
Cheers,
Jon
Thanks Jon! Super helpful. We'll give it a go and let you know how it's working out.
Hi Jon,
Again, thank you for responding...we really didn't know how to proceed. Unfortunately, the code didn’t work (no particles appeared at all) after setting the script to an Empty object and assigning the two particle materials, but there were no error messages in the logger which is good. We did a little debugging and it seems to be infinitely repeating the function update () and never getting beyond that point.
Can you suggest a fix?
Also, can you elaborate on where and how should we call resetSequence()?
Many thanks!
Hi Ian!
Glad to hear it's getting somewhere! Yes It's expected that the update script is running every time since it needs to keep track of time.
It seems like it's not doing anything because resetSequence is not being called. You should call resetSequence wherever you are telling your animation to start.
For example:
Let me know if you need me to clarify :)