I have a GameObject which is parent of another GameObject which is parent of an animated model. This structure is essential to the game flow, so I can’t change it.
Is there a way to sync the model’s animation through UNET? Apparently, there’s no such thing as “Network Animator Child”.
After looking at the SourceCode for the NetworkAnimator. I fail to see why you can’t use the NetworkAnimator for child objects? What happends if you try?
It automatically makes a NetworkIdentity component when you add a NetworkAnimator and I get a message saying a gameobject can only have one NetworkIdentity component and it should be at that GO’s root
My player prefab hierachy: Parent Network Object → Gfx object with animator and my animation script.
Firstly, If you want to delegate authority to clients, so they can propogate their animation states to server, you need to make script, that extends Network Animator and overrides “OnIsServerAuthoritative()”, so It only returns false:
public class OwnerNetworkAnimator : NetworkAnimator
{
protected override bool OnIsServerAuthoritative()
{
return false;
}
}
But anyway I’ve got syncing issue, host does not propogate animation states to client, and vice versa. What I did is put if statement in my animable script on Gfx object, so It updates animation properties only for owner:
using Unity.Netcode;
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class PlayerAnimable : NetworkBehaviour
{
[SerializeField] private string _movementVelocityArgument;
[SerializeField] private PlayerMovable _playerMovable;
private Animator _animator;
private int _movementVelocityHashed;
private void Awake()
{
_animator = GetComponent<Animator>();
}
private void Start()
{
_movementVelocityHashed = Animator.StringToHash(_movementVelocityArgument);
_movementVelocityArgument = null;
}
void Update()
{
if (!IsOwner)
{
return;
}
#region ComponentNullCheck
if (!_playerMovable)
{
Debug.LogError(name + "does not have PlayerMovable component, but tries to use It");
return;
}
if (!_animator)
{
Debug.LogError(name + "does not have Animator component, but tries to use It");
return;
}
#endregion
_animator.SetFloat(_movementVelocityHashed, _playerMovable.MovementDirection.magnitude);
}
}