Search Unity

ClientNetworkTransform + NetworkAnimator works?

Discussion in 'Netcode for GameObjects' started by Devseba, Apr 29, 2022.

  1. Devseba

    Devseba

    Joined:
    Feb 26, 2021
    Posts:
    9
    Hi,

    I'm learning, sorry if the question seems obvious.

    - I have the ClientNetworkTransform working: players are moving and syncing.
    - I have walking animations working in a "manual way": not using the NetworkAnimator because I couldn't make it work.

    Question: is the NetworkAnimator suposed to work like the ClientNetworkTransform regarding NetworkVariables? With ClientNetworkTransform I don't have to set any NetworkVariables. I think with a working NetworkAnimator, the attached script will no longer need the animation code.

    The script attached (that is spawned with the player) handles 2 things:
    1. Starts the spawned player with a hero selection.
    2. Manages walking animation in a "manual way".

    It is not like i wrote a lot of code to make the animations work. But I did tried like 20 hours do make the NetworkAnimator work without luck. And I will miss a lot of functionality if I don't use it.

    EDIT 1: I'm still trying to make the NetworkAnimator work. In the moment Vilmer selects the Animator, the parameters are shown in the component. I'm not getting that behaviour. Maybe the issue is there (or different NetworkAnimator). Check video at 5:50s:


    EDIT 2: the component behaviour mentioned in "EDIT 1" changed in version 1.0.0-pre.6. Vilmer used 1.0.0-pre.3. It worked in version 4 and 5. But the changes made in v6 hides that behaviour. I don't know if it is intended to not show the parameters but can be an indicator of the problem.
    I'm using the current latest version pre.8.

    EDIT 3: ok, bug was created about this. https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues/1348

    Thanks for help,
    Regards.

    Code (CSharp):
    1.  
    2. public class PlayerNetwork : NetworkBehaviour
    3. {
    4.     private NetworkVariable<byte> selectedHeroe = new NetworkVariable<byte>(255);
    5.     private NetworkVariable<float> playerSpeed = new NetworkVariable<float>(0);
    6.  
    7.     private Animator animator;
    8.     private byte heroe;
    9.  
    10.     // Called by all previous objects, and new ones 1 by 1
    11.     public override void OnNetworkSpawn()
    12.     {
    13.         // Need to subscribe to every player changes:
    14.         selectedHeroe.OnValueChanged += OnPlayerHeroeChange;
    15.         playerSpeed.OnValueChanged += OnPlayerSpeedChange;
    16.  
    17.         if (IsLocalPlayer)
    18.         {
    19.             transform.position = new Vector3(1, 1, 1);
    20.             GetComponent<Player>().PlayerSpawnSettings();
    21.  
    22.             heroe = (byte) UnityEngine.Random.Range(0, Enum.GetValues(typeof(AssetManager.Heroe)).Length);
    23.             SetPlayerHeroeServerRpc(heroe);
    24.         }
    25.         else GetComponent<Player>().SetHeroe(selectedHeroe.Value);
    26.     }
    27.  
    28.     private void OnPlayerHeroeChange(byte previousHeroe, byte newHeroe)
    29.     {
    30.         GetComponent<Player>().SetHeroe(newHeroe);
    31.     }
    32.  
    33.     [ServerRpc]
    34.     public void SetPlayerHeroeServerRpc(byte heroe)
    35.     {
    36.         selectedHeroe.Value = heroe;
    37.     }
    38.  
    39.     private void OnPlayerSpeedChange(float previousSpeed, float newSpeed)
    40.     {
    41.         animator.SetFloat("speed", playerSpeed.Value);
    42.     }
    43.  
    44.     [ServerRpc]
    45.     public void UpdatePlayerSpeedServerRpc(float newSpeed)
    46.     {
    47.         playerSpeed.Value = newSpeed;
    48.     }
    49.  
    50.     public void SetAnimator(Animator anim)
    51.     {
    52.         animator = anim;
    53.     }
    54.  
    55.     public override void OnNetworkDespawn()
    56.     {
    57.         selectedHeroe.OnValueChanged -= OnPlayerHeroeChange;
    58.         playerSpeed.OnValueChanged -= OnPlayerSpeedChange;
    59.     }
    60. }
    61.  
     
    Last edited: Apr 30, 2022