Search Unity

Question Client controlling server object

Discussion in 'Multiplayer' started by Haruwolf, Nov 4, 2021.

  1. Haruwolf

    Haruwolf

    Joined:
    Oct 15, 2021
    Posts:
    3
    So I'm trying to create something that on theory is simple: Server spawn the object and the client control the object that's updated on server and other clients.

    However I can't even get client control the object to update on server, I can get at maximum the client modifying his own variable. I tried RPC, trying to put Client Network Transform component at object, Network Variable, none seems work.

    Code (CSharp):
    1. public NetworkVariable<Vector3> position = new NetworkVariable<Vector3>();
    2.         public Vector3 clientVector = new Vector3(0, 0, 0);
    3.  
    4. public void Move()
    5.         {
    6.             if (IsClient)
    7.             {
    8.                 clientVector = RandomPos();
    9.                 UpdatePosServerRpc(clientVector);
    10.             }
    11.  
    12.         }
    13.  
    14.         [ServerRpc]
    15.         void UpdatePosServerRpc(Vector3 cV, ClientRpcParams rpcParams = default)
    16.         {
    17.             position.Value = clientVector;
    18.             transform.position = position.Value;
    19.  
    20.         }
    21.  
    22.             Vector3 RandomPos()
    23.         {
    24.             //Generate random position
    25.             return new Vector3(Random.Range(-3f, 3f), 1f, Random.Range(-3f, 3f));
    26.         }
    The object is spawned by server before this code happens, the server don't happens to appear any message error.
     
    Sightwalker likes this.
  2. Sightwalker

    Sightwalker

    Joined:
    Jan 7, 2015
    Posts:
    4
    I'm having the same problem. Tried to modify the module one Golden Path code to control the objects on both server and client but I only managed to make it work on the server build. Tried to use both ServerRpc and ClientRpc and it didn't seem to make a difference.

    Also, the Netcode documentation was very poorly updated to match the changes from MLAPI to Netcode until the moment I type this. It's very common to run through pieces of code that are still using the MLAPI syntax (like the NetworkVariable example), which really makes learning it difficult and/or slow.
     
    Haruwolf likes this.
  3. herrmutig

    herrmutig

    Joined:
    Nov 30, 2020
    Posts:
    22
    Is this intended in your ServerRPC Method:

    position.Value = clientVector;? Why do you have the Vector3 cv parameter if unused?
     
    Sightwalker likes this.