Search Unity

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

Question Netcode animation not working

Discussion in 'Netcode for GameObjects' started by unity_VngtZy77FXMDEg, May 3, 2023.

  1. unity_VngtZy77FXMDEg

    unity_VngtZy77FXMDEg

    Joined:
    Sep 1, 2021
    Posts:
    5
    Hi the client side animations doesnt work.
    Animation for host works perfectly (client and host side can see the animations of the host).
    Im also logging the speed variable which is used in the animator but it is for the client side always 0.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Unity.Netcode;
    5. using UnityEngine;
    6. using UnityEngine.AI;
    7.  
    8. public class PlayerNetwork : NetworkBehaviour
    9. {
    10.     public NavMeshAgent agent;
    11.     public float rotateSpeedMovement = 0.05f;
    12.     private float rotateVelocity;
    13.  
    14.  
    15.     public GameObject animGameObject;
    16.     public Animator anim;
    17.     float motionSmoothTime = 0.1f;
    18.    
    19.  
    20.     private void Start()
    21.     {
    22.         anim = animGameObject.GetComponent<Animator>();
    23.         agent = GetComponent<NavMeshAgent>();
    24.        
    25.     }
    26.     void Update()
    27.     {
    28.         if (!IsOwner) return;
    29.        
    30.         if (Input.GetMouseButtonDown(1))
    31.         {
    32.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    33.  
    34.                 MoveServerRPC(ray);          
    35.         }
    36.  
    37.         float speed = agent.velocity.magnitude / agent.speed;
    38.         Debug.Log(speed);
    39.         AnimationServerRPC(speed);
    40.     }
    41.     [ServerRpc]
    42.     private void MoveServerRPC(Ray ray)
    43.     {
    44.         RaycastHit hit;
    45.         if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    46.         {
    47.             if(hit.collider.tag =="Ground")
    48.             {
    49.                 //movement
    50.                 agent.SetDestination(hit.point);
    51.                 agent.stoppingDistance = 0;
    52.  
    53.                 //rotation
    54.                 Quaternion rotationToLookAt = Quaternion.LookRotation(hit.point - transform.position);
    55.                 float rotationY = Mathf.SmoothDampAngle(transform.eulerAngles.y,
    56.                     rotationToLookAt.eulerAngles.y,
    57.                     ref rotateVelocity,
    58.                     rotateSpeedMovement * (Time.deltaTime * 5));
    59.  
    60.                 transform.eulerAngles = new Vector3(0, rotationY, 0);
    61.             }
    62.         }
    63.     }
    64.     [ServerRpc]
    65.     private void AnimationServerRPC(float speed)
    66.     {
    67.        
    68.         anim.SetFloat("Speed", speed, motionSmoothTime, Time.deltaTime);
    69.         //Debug.Log(speed);
    70.     }
    71.  
    72.  
    73.  
    74.  
    75.     private void Animantion(float speed)
    76.     {
    77.         //float speed = agent.velocity.magnitude / agent.speed;
    78.         anim.SetFloat("Speed", speed, motionSmoothTime, Time.deltaTime);
    79.     }
    80.  
    81.     public void Move()
    82.     {
    83.         if(!IsOwner) return;
    84.         if (Input.GetMouseButtonDown(1))
    85.         {
    86.             RaycastHit hit;
    87.            
    88.             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
    89.             {
    90.                 if (hit.collider.tag == "Ground")
    91.                 {
    92.                     //movement
    93.                     agent.SetDestination(hit.point);
    94.                     agent.stoppingDistance = 0;
    95.  
    96.                     //rotation
    97.                     Quaternion rotationToLookAt = Quaternion.LookRotation(hit.point - transform.position);
    98.                     float rotationY = Mathf.SmoothDampAngle(transform.eulerAngles.y,
    99.                         rotationToLookAt.eulerAngles.y,
    100.                         ref rotateVelocity,
    101.                         rotateSpeedMovement * (Time.deltaTime * 5));
    102.  
    103.                     transform.eulerAngles = new Vector3(0,rotationY, 0);  
    104.                 }
    105.             }
    106.         }
    107.     }
    108. }
    109.  
     
  2. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244