Search Unity

Move a non-player object with client

Discussion in 'Multiplayer' started by Di_o, May 14, 2019.

  1. Di_o

    Di_o

    Joined:
    May 10, 2016
    Posts:
    62
    I know, this stuff is deprfecated and this quastion is out there in a billion ways. Still, I can't find anyting that helps me out. It really should not be that hard, but I'm struggeling.

    I'm spawning an object, that can be scaled and rotated by all players (UNET). Everything works fine on the server side. The host can rotate and scale it and it's perfectly syncing on the client side. It's just simply not working the other way around. There is supposed to be some magic authority that allows clients to do this. I just cant figure out how. Please help.

    It's driving me mad...

    Code (CSharp):
    1.  
    2.     [NetworkSettings(sendInterval = 0.033f)]
    3.     public class CollabObject_RotScale : NetworkBehaviour
    4.     {
    5.  
    6.         private Transform uiRootPos, body;
    7.  
    8.         [SyncVar(hook = "OnRotUpdate")] Quaternion rot;
    9.         [SyncVar(hook = "OnScaleUpdate")] float scale;
    10.  
    11.         private void Awake()
    12.         {
    13.             uiRootPos = GameObject.Find("UIRootPos").transform;  
    14.             body = transform.GetChild(0);
    15.             scale = body.localScale.x;
    16.             transform.position = uiRootPos.position;
    17.         }
    18.  
    19.         void Update()
    20.         {
    21.             if(body.transform.localScale.x != scale)
    22.             {              
    23.                 scale = body.localScale.x;              
    24.                 CmdUpdateScale(body.localScale.x);
    25.             }
    26.  
    27.             if(body.transform.localRotation != rot)
    28.             {              
    29.                 rot = body.localRotation;              
    30.                 CmdUpdateRot(body.localRotation);
    31.             }
    32.         }
    33.  
    34.         void OnRotUpdate(Quaternion newRot)
    35.         {
    36.             rot = newRot;
    37.             body.localRotation = newRot;
    38.         }
    39.  
    40.         void OnScaleUpdate(float newScale)
    41.         {
    42.             scale = newScale;
    43.             body.localScale = Vector3.one * scale;
    44.         }
    45.  
    46.         [Command]
    47.         void CmdUpdateRot(Quaternion newRot)
    48.         {
    49.             RpcUpdateRot(newRot);
    50.         }
    51.  
    52.         [Command]
    53.         void CmdUpdateScale(float newScale)
    54.         {
    55.             RpcUpdateScale(newScale);
    56.         }
    57.  
    58.         [ClientRpc]
    59.         void RpcUpdateRot(Quaternion newRot)
    60.         {
    61.             rot = newRot;
    62.         }
    63.  
    64.         [ClientRpc]
    65.         void RpcUpdateScale(float newScale)
    66.         {
    67.             scale = newScale;
    68.         }
    69.     }
    70.  
    71.