Hello, I’m a beginner with animation and netcode so I searched this subject for a couple of days… I’m already able to swap animation clips on runtime, my objective is to have the same logic to fire an action but with different animation clips, which will be selected by the actual equip on my character.
It was a sucess but I can’t get to think a way to sync these swaps over the clients. It only work on the LocalPlayer-side.
The transition to the state that will be swaped clips occurs via a trigger, but I guess that wasn’t supposed to be a problem since the network animator is owner authorative.
Here the code to swap the animation clip.
public class Teste : StateMachineBehaviour
{
private AnimationClip selectedClip = null;
private Animator cAnimator = null;
private bool isActive = false;
private int layerIndex;
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int _layerIndex)
{
layerIndex = _layerIndex;
// Debug.Log("OnStateEnterFired on layer: " + layerIndex);
this.cAnimator = this.cAnimator ? this.cAnimator : animator;
isActive = true;
AnimatorClipInfo[] animatorClipInfos = cAnimator .GetNextAnimatorClipInfo(_layerIndex);
if(selectedAnimationClip.name == animatorClipInfos[0].clip.name) return;
AnimatorOverrideController aoc = new AnimatorOverrideController(this.cAnimator .runtimeAnimatorController);
aoc[animatorClipInfos[0].clip.name] = selectedClip;
cAnimator .runtimeAnimatorController = aoc;
}
public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
isActive = false;
}
}
“Dicionary path”:
I’m thinking on using “OnStateEnter” to execute an outside script to all clients and host and this scripts fire back the swap function, but since RPC can’t pass AnimationClips as argument I’ll problably need to build a dicionary with IDs and AnimationClips and the lag will problably accumulate.
Is there a simple solution to that or the “dicionary path” is a good way?