Search Unity

Server object transport syncs, but not the client one

Discussion in 'Multiplayer' started by Cafn, Feb 3, 2016.

  1. Cafn

    Cafn

    Joined:
    Jan 8, 2015
    Posts:
    19
    Hi there.

    So i've implemented multiplayer on my game and so far, everything is working fine.

    Only problem right now is to sync the movement on both host and client.

    I got 2 prefabs. Both of them have network identity (local player authority) and network transform.
    - Player prefab that is set on the Network Manager.
    - Worker prefab is set in Registed Spawnable Prefabs.

    At the start of player script, I have:

    Code (CSharp):
    1.   void Start()
    2.     {
    3.         hud = GetComponentInChildren<HUD>();
    4.         AddStartResourceLimits();
    5.         AddStartResources();
    6.         if (isLocalPlayer)
    7.         {
    8.             CmdSpawnThings();
    9.         }
    10.     }
    11.  
    12.     [Command] //Calls server to do something
    13.     void CmdSpawnThings()
    14.     {
    15.         GameObject instance = Instantiate(workerPrefab, transform.position, transform.rotation) as GameObject;
    16.         instance.transform.parent = transform;
    17.         //NetworkServer.Spawn(instance);
    18.         NetworkServer.SpawnWithClientAuthority(instance,base.connectionToClient);
    19.         //RpcSetParent(instance);
    20.     }

    I'm not sure if i'm missing anything to get this to work, but moving the worker in the client doesnt update it's location on the host.

    I've seen several tutorials and examples and I guess i'm not missing anything.

    Thanks for your time
     
  2. Cafn

    Cafn

    Joined:
    Jan 8, 2015
    Posts:
    19
    Ok, after few hours of trying, getting desperate and write this post, figured it out (sometimes, we just have to step back and think a little bit about it).

    So on my code, when I right clicked to make the unit move, just had to send a callback to the server

    Code (CSharp):
    1.     [Command]
    2.     public void CmdSetTarget(Vector3 target)
    3.     {
    4.         RpcSetTarget(target);
    5.     }
    6.     [ClientRpc]
    7.     public void RpcSetTarget(Vector3 target)
    8.     {
    9.         agent.SetDestination(target);
    10.     }
    And it worked.. Really though that the network transform was enough to sync the position from both sides.