- Home /
Using cycle offset feature inside of animator?
Theirs an "OffsetAnimation" parameter inside the animator that you can assign to animation states.
https://docs.unity3d.com/ScriptReference/Animations.AnimatorState-cycleOffsetParameter.html
I'm unsure how to effect that number to use it to offset a character's idle or the like should I want to spawn several at the same time. (the goal to have them all play their animation with an offset so they don't all look the synched up with each other.
Has anyone used this?
Thanks!
Answer by ionside · Sep 09, 2016 at 01:12 AM
I need this a lot in crowd animations etc. and generally use something like:
anim = GetComponent<Animator>();
randomIdleStart = Random.Range(0,anim.GetCurrentAnimatorStateInfo(0).length); //Set a random part of the animation to start from
anim.Play("Crow|Idle", 0, randomIdleStart);
Where you get the length of the animation, then randomly start at any point of it.
Ok this works! However I find that if the values random comes up with are too close together, let's say within 0-5 frames of the each other, the difference is negligible.
What I've done to help them differ is to randomize the Animator.speed value as well slightly.
If you can get away altering the speed slightly it goes a long way!
Answer by ttesla · Mar 19, 2018 at 02:33 PM
You can use parameter for modifying Cycleoffset value. Make a "float" parameter. (Cycleoffset's valid range is [0.0 - 1.0].) Then assign the parameter from Inspector screen.
And from code, set your parameter:
GetComponent<Animator>().SetFloat("Offset", Random.Range(0.0f, 1.0f));
Setting the parameter doesn't work for me. Any ideas why? I have only one (idle) state which is looped, the offset parameter is correctly set (in Awake()), but it doesn't have any effect.