- Home /
Shuriken, change Random Between Two Constants by script
For several parameters, Shuriken is giving different ways to affect it in the editor : constant, curve, random, gradient etc. However, by script you can only affect a constant value, like for instance :
system.startSize = 5f; // Can't specify a curve or a range, it's a float !
Is there some secret ninjas ways to do that, or do I need to do every frames :
// Or curve.Evaluate(system.time/system.duration)
system.startSize = Random.Range(min, max);
As we are talking, among other things, about Shuriken's random stuff, here is an interesting paper (unrelated to the issue here, and yet kind of, as the result above won't be similar to the builtin one, because the random value is re-sampled, thus not connected).
[Edit] Apparently, when a float parameter is set "Random Between Two Constants", affecting a new value will set it as the second constant, and the first will then be firstValue * (lastSecondValue / newSecondValue). For instance, you set startSize in the inspector between 1 and 3. Then in Awake, you do startSize = 5; startSize is now between 1.66 and 5. It's empirical, so take it carefully. I didn't found any ways to set both values, or curves, gradient, two curves or two gradients.
I've found the script interface leaves some room for improvement - for example, interpolating a particle system between two states seems difficult. Sorry I can't help you, but want to note that I also struggle with this.
I hope they are going to improve it in further versions, that's probably only the first step.
Answer by yoyo · Jun 23, 2012 at 06:11 AM
Shuriken is a curiously dark box when looked at from a scripting perspective -- there are many parameters you can manipulate in the editor that are not accessible from script, even things as simple as the size of the emitter.
The best Secret Ninja Sauce(TM) I've found is to use the editor to create an animation that animates a particle system property from a minimum value at the start of the animation to a maximum value at the end of the animation, then from your script you can update the animation state using normalizedTime to select a value between min and max (0 for min, 1 for max).
You can get the animation state like this (probably best to do this in Awake or Start) ...
AnimationState setScaleAnim = animation["setScale"];
And then set your value to (say) halfway between min and max with ...
setScaleAnim.normalizedTime = 0.5f;
You can set up a bunch of animation states, one for each parameter (or set of parameters) that you want to procedurally control.
It's a little indirect, but it does give you scripted control of anything you can see in the Shuriken editor.
I haven't tried it, but for bonus points it should even be possible to create and add AnimationClips procedurally, for full control.