Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Client wont move with a moving platform, but Host works

Discussion in 'Netcode for GameObjects' started by YashaISR, May 8, 2023.

  1. YashaISR

    YashaISR

    Joined:
    Nov 8, 2016
    Posts:
    16
    So im trying to build a multiplayer game where there is a moving platform, both client and host can activate the moving platform, but only the host moves with the platform, both have the same code
    I did some testing and it seem that it has something to do with the fact that I'm using the
    "Multiplayer Sample Utilities" that include "Client Network Transform"
    which only overrides

    Code (CSharp):
    1. using Unity.Netcode.Components;
    2. using UnityEngine;
    3.  
    4. namespace Unity.Multiplayer.Samples.Utilities.ClientAuthority
    5. {
    6.     /// <summary>
    7.     /// Used for syncing a transform with client side changes. This includes host. Pure server as owner isn't supported by this. Please use NetworkTransform
    8.     /// for transforms that'll always be owned by the server.
    9.     /// </summary>
    10.     [DisallowMultipleComponent]
    11.     public class ClientNetworkTransform : NetworkTransform
    12.     {
    13.         /// <summary>
    14.         /// Used to determine who can write to this transform. Owner client only.
    15.         /// This imposes state to the server. This is putting trust on your clients. Make sure no security-sensitive features use this transform.
    16.         /// </summary>
    17.         protected override bool OnIsServerAuthoritative()
    18.         {
    19.             return false;
    20.         }
    21.     }
    22. }
    23.  
    if I'm using the regular component "Network transform" it works fine but I want a more fluid movement and need the movement be client authoritative

    Attaching Also a video for reference
    Video : https://www.dropbox.com/t/KVXFnrUjnYNeTkMa
     
  2. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    162
    Yes, because Netcode has Server Authoritative model.
    I want to recommend you CodeMonkey tutorial for essential video.
     
  3. YashaISR

    YashaISR

    Joined:
    Nov 8, 2016
    Posts:
    16
    Thats exactly the video I was following, and that's the result, remote client wont move with a moving platform
     
  4. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    60
    YashalSR,
    Could you provide more information about the moving platform?
    Is that a server authoritative (i.e. default) NetworkTransform and if so are you using a standard Rigidbody with a NetworkRigidbody? If so, then you are most likely going to need to detect that you are "on the platform" on the client side and adjust the client's position.
    Most likely just creating your own version of the Multiplayer Sample Utilities ClientNetworkTransform and handle applying the platform's delta offset yourself. It might look something like:
    Code (CSharp):
    1.     [DisallowMultipleComponent]
    2.     public class ClientNetworkTransform : NetworkTransform
    3.     {
    4.         private bool m_IsOnPlatform;
    5.         private Transform m_PlatformTransform;
    6.         private Vector3 m_PreviousPlatformPosition;
    7.  
    8.         public void PlayerOnPlatform(Transform platformTransform)
    9.         {
    10.             if (!IsOwner)
    11.             {
    12.                 return;
    13.             }
    14.  
    15.             m_IsOnPlatform = true;
    16.             m_PlatformTransform = platformTransform;
    17.             m_PreviousPlatformPosition = m_PlatformTransform.position;
    18.         }
    19.  
    20.         public void PlayerOffPlatform()
    21.         {
    22.             if (!IsOwner)
    23.             {
    24.                 return;
    25.             }
    26.             m_IsOnPlatform = false;
    27.         }
    28.  
    29.         /// <summary>
    30.         /// Used to determine who can write to this transform. Owner client only.
    31.         /// This imposes state to the server. This is putting trust on your clients. Make sure no security-sensitive features use this transform.
    32.         /// </summary>
    33.         protected override bool OnIsServerAuthoritative()
    34.         {
    35.             return false;
    36.         }
    37.  
    38.         protected override void Update()
    39.         {
    40.             // if the owner is on the platform, then update the client player's position based on the platform's motion
    41.             if (m_IsOnPlatform)
    42.             {
    43.                 // Add the delta position of the platform's position to the owner's position
    44.                 transform.position += m_PlatformTransform.position - m_PreviousPlatformPosition;
    45.                 // Store off the platform's position for next frame delta calculations
    46.                 m_PreviousPlatformPosition = m_PlatformTransform.position;
    47.             }
    48.             base.Update();
    49.         }
    50.     }
    The above is more "pseudo" code and you might have better ideas on how/where you want to apply the platform's position delta, but it is the general idea of how you can handle this scenario. If the platform is server authoritative and interpolation is enabled, then the delta position of the platform should be "relatively already smooth".