- Home /
Multiple animators use the same controller / sate-machine or how to sync them?
This seems simple but I can't get it to work out....
I need multiple animators that all use the same controller asset, to have their state machines be synchronized, regardless of if they are enabled or not.
I almost need a "global" state machine that any instance of this animation reads from, but I don't know how to do it.
I have a system that creates a level out of prefabs. Each prefab contains 3 copies for the same fbx(model and animation). They all use the same Animator Controller aka Animator State-Machine.
However, because each has it's own unique animator component, and apparently it's own instance of the state machine, I'm running into trouble.
I can track and access all of my animators to set triggers on each one, however not all of their objects are on at the same time. So I can synchronize all the state machines that are on at the same time, but when new ones turn on, they are in their default "idle" state.
Answer by smallbit · May 15, 2015 at 09:53 AM
You can use This method. With this you can set the animation state start time, than any time you enable new component you will sync its animation with current. Maybe you will need to have some global timer to know where you animation is at the moment which would reset everytime animations makes a loop (can use an animation event for that).
This should do the trick if I am understanding you correctly.
I think I'm going to set up a global instance of the animated object and feed it's state and time values to that play method when each new animator is turned on.
I just need to read the docs some more on how to get the state info from the "master" animator.
Thanks!
Answer by Crymorph · May 15, 2015 at 02:02 PM
I could see this being done through a StateMachineBehavior script using the SharedBetweenAnimatorsAttribute (Documentation on this can be found Here) Although it is intended to reduce the memory footprint it also seems to be capable of doing what you are looking for. You would attach the behavior script to the default start state and it could look something like this (untested):
using UnityEngine;
using System.Collections;
[SharedBetweenAnimators]
public class SyncBehav : StateMachineBehaviour {
//Used to sync
public Animator masterAnimatorComponent;
// OnStateEnter is called when entering a state
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex){
//I am sure this can be placed somewhere else but for demo purposes it's here
if (!masterAnimatorComponent) {
masterAnimatorComponent = animator;
} else {
//Sync any required params, states or clips here.
}
}
}
Very interesting.
I'm not sure I understand completely but am going to look into this further....
I'm fuzzy on exactly how I would sync the states from this state.
I don't think I'm understanding the sharedbetweenattribute or statmachine behaviors correctly....
It would essentially be an additional state in the state machine for my animator, correct?
It would be the entry state, that then would set the appropriate state when each new animator instance spawns based on the current state of the master, or am I misunderstanding?
Would I actually want the SharedBetweenAnimatorsAttribute at that point though?