Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Sync child's animation through network

Discussion in 'Multiplayer' started by eduardo_cola42, Nov 12, 2016.

  1. eduardo_cola42

    eduardo_cola42

    Joined:
    Nov 2, 2016
    Posts:
    11
    Hello,

    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".

    Any help is appreciated.
     
  2. thejet238

    thejet238

    Joined:
    Jul 8, 2017
    Posts:
    3
    I would like to bump this. I am having the same exact issue.
     
  3. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Should only require minor tweaks of the NetworkAnimator to fix
     
  4. thejet238

    thejet238

    Joined:
    Jul 8, 2017
    Posts:
    3
    @TwoTen Would you happen to know what those tweaks would be?
     
  5. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    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?
     
  6. thejet238

    thejet238

    Joined:
    Jul 8, 2017
    Posts:
    3
    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
     
  7. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Yes? So do what they say. And put the NetworkANimator on the root object. Then drag the child animator object to it?
     
    PloyMetha, chrismarch and xVergilx like this.
  8. unity_lSXJSIZAvqe3Qw

    unity_lSXJSIZAvqe3Qw

    Joined:
    Jan 30, 2020
    Posts:
    1
    I've had same issue, what helped me:

    1. My player prefab hierachy: Parent Network Object -> Gfx object with animator and my animation script.
    2. 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:

    Code (CSharp):
    1. public class OwnerNetworkAnimator : NetworkAnimator
    2. {
    3.     protected override bool OnIsServerAuthoritative()
    4.     {
    5.         return false;
    6.     }
    7. }
    8.  
    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:

    Code (CSharp):
    1. using Unity.Netcode;
    2. using UnityEngine;
    3.  
    4. [RequireComponent(typeof(Animator))]
    5. public class PlayerAnimable : NetworkBehaviour
    6. {
    7.     [SerializeField] private string _movementVelocityArgument;
    8.     [SerializeField] private PlayerMovable _playerMovable;
    9.     private Animator _animator;
    10.  
    11.     private int _movementVelocityHashed;
    12.  
    13.     private void Awake()
    14.     {
    15.         _animator = GetComponent<Animator>();
    16.     }
    17.  
    18.     private void Start()
    19.     {
    20.         _movementVelocityHashed = Animator.StringToHash(_movementVelocityArgument);
    21.  
    22.         _movementVelocityArgument = null;
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         if (!IsOwner)
    28.         {
    29.             return;
    30.         }
    31.  
    32.         #region ComponentNullCheck
    33.         if (!_playerMovable)
    34.         {
    35.             Debug.LogError(name + "does not have PlayerMovable component, but tries to use It");
    36.             return;
    37.         }
    38.  
    39.         if (!_animator)
    40.         {
    41.             Debug.LogError(name + "does not have Animator component, but tries to use It");
    42.             return;
    43.         }
    44.         #endregion
    45.  
    46.         _animator.SetFloat(_movementVelocityHashed, _playerMovable.MovementDirection.magnitude);
    47.     }
    48. }
    49.  
    Root object setup:

    upload_2023-8-7_12-49-32.png

    Gfx object:
    upload_2023-8-7_12-49-59.png

    Hope It might help someone!