Search Unity

Child prefab not replicating correclty

Discussion in 'Multiplayer' started by jdg23, Apr 1, 2023.

  1. jdg23

    jdg23

    Joined:
    Feb 10, 2020
    Posts:
    16
    Unity 2021.3.16f1

    Hi,

    I have a vehicle with a network object component and a network rigidbody.

    Most of the vehicle behavior is correclty replicated from host to client and client to host except the wheels that tends to take very strange rotation positions.

    The wheels are children of the car prefab.

    I tried to add a client network transform on the wheels and set a rotation angle threshold but it does not solve the problem

    Here's the code that handles stearing the front wheels and animating all four wheels also :

    Code (CSharp):
    1.  public void TurnLeft()
    2.     {
    3.       if (!IsOwner) return;
    4.      
    5.       steeringAxis = steeringAxis - (Time.deltaTime * 10f * steeringSpeed);
    6.       if(steeringAxis < -1f){
    7.         steeringAxis = -1f;
    8.       }
    9.       var steeringAngle = steeringAxis * maxSteeringAngle;
    10.       frontLeftCollider.steerAngle = Mathf.Lerp(frontLeftCollider.steerAngle, steeringAngle, steeringSpeed);
    11.       frontRightCollider.steerAngle = Mathf.Lerp(frontRightCollider.steerAngle, steeringAngle, steeringSpeed);
    12.     }
    13.  
    14.     //The following method turns the front car wheels to the right. The speed of this movement will depend on the steeringSpeed variable.
    15.     public void TurnRight()
    16.     {
    17.       if (!IsOwner) return;
    18.      
    19.       steeringAxis = steeringAxis + (Time.deltaTime * 10f * steeringSpeed);
    20.       if(steeringAxis > 1f){
    21.         steeringAxis = 1f;
    22.       }
    23.       var steeringAngle = steeringAxis * maxSteeringAngle;
    24.       frontLeftCollider.steerAngle = Mathf.Lerp(frontLeftCollider.steerAngle, steeringAngle, steeringSpeed);
    25.       frontRightCollider.steerAngle = Mathf.Lerp(frontRightCollider.steerAngle, steeringAngle, steeringSpeed);
    26.     }
    27.  
    28.     //The following method takes the front car wheels to their default position (rotation = 0). The speed of this movement will depend
    29.     // on the steeringSpeed variable.
    30.     public void ResetSteeringAngle()
    31.     {
    32.       if (!IsOwner) return;
    33.      
    34.       if(steeringAxis < 0f){
    35.         steeringAxis = steeringAxis + (Time.deltaTime * 10f * steeringSpeed);
    36.       }else if(steeringAxis > 0f){
    37.         steeringAxis = steeringAxis - (Time.deltaTime * 10f * steeringSpeed);
    38.       }
    39.       if(Mathf.Abs(frontLeftCollider.steerAngle) < 1f){
    40.         steeringAxis = 0f;
    41.       }
    42.       var steeringAngle = steeringAxis * maxSteeringAngle;
    43.       frontLeftCollider.steerAngle = Mathf.Lerp(frontLeftCollider.steerAngle, steeringAngle, steeringSpeed);
    44.       frontRightCollider.steerAngle = Mathf.Lerp(frontRightCollider.steerAngle, steeringAngle, steeringSpeed);
    45.     }
    46.  
    47.     // This method matches both the position and rotation of the WheelColliders with the WheelMeshes.
    48.     void AnimateWheelMeshes()
    49.     {
    50.  
    51.       if (!IsOwner) return;
    52.      
    53.       try{
    54.         Quaternion FLWRotation;
    55.         Vector3 FLWPosition;
    56.         frontLeftCollider.GetWorldPose(out FLWPosition, out FLWRotation);
    57.         frontLeftMesh.transform.position = FLWPosition;
    58.         frontLeftMesh.transform.rotation = FLWRotation;
    59.  
    60.         Quaternion FRWRotation;
    61.         Vector3 FRWPosition;
    62.         frontRightCollider.GetWorldPose(out FRWPosition, out FRWRotation);
    63.         frontRightMesh.transform.position = FRWPosition;
    64.         frontRightMesh.transform.rotation = FRWRotation;
    65.  
    66.         Quaternion RLWRotation;
    67.         Vector3 RLWPosition;
    68.         rearLeftCollider.GetWorldPose(out RLWPosition, out RLWRotation);
    69.         rearLeftMesh.transform.position = RLWPosition;
    70.         rearLeftMesh.transform.rotation = RLWRotation;
    71.  
    72.         Quaternion RRWRotation;
    73.         Vector3 RRWPosition;
    74.         rearRightCollider.GetWorldPose(out RRWPosition, out RRWRotation);
    75.         rearRightMesh.transform.position = RRWPosition;
    76.         rearRightMesh.transform.rotation = RRWRotation;
    77.       }catch(Exception ex){
    78.         Debug.LogWarning(ex);
    79.       }
    80.     }
    81.  

    Thanks