Search Unity

Need help with sharing commands with client

Discussion in 'Multiplayer' started by ITZZZETHAN, Jul 20, 2017.

  1. ITZZZETHAN

    ITZZZETHAN

    Joined:
    Sep 10, 2015
    Posts:
    28
    I am making a game and I am trying to make it so not only the host of the server can see when you press "A" and a gameobject sets active and when you press "D" another gameobject setsactive. I have tried but am not succeeding :( here is my script
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6. public class Movement : NetworkBehaviour
    7. {
    8.     public float movementspeed = 1f;
    9.     public float coolDown = 0.2f;
    10.     public float coolDownTimer;
    11.     [SyncVar]
    12.     public GameObject Joust1;
    13.     [SyncVar]
    14.     public GameObject Joust2;
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.        
    19.     }
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (!isLocalPlayer)
    24.         {
    25.             return;
    26.         }
    27.         if (coolDownTimer > 0)
    28.         {
    29.             coolDownTimer -= Time.deltaTime;
    30.         }
    31.         if (coolDownTimer < 0)
    32.         {
    33.             coolDownTimer = 0;
    34.         }
    35.         if (Input.GetMouseButton(0))
    36.         {
    37.             transform.Translate(Vector2.up * 0 * Time.deltaTime);
    38.             transform.Translate(Vector2.right * 0f * Time.deltaTime);
    39.             transform.Translate(Vector2.left * 0f * Time.deltaTime);
    40.         }
    41.         else
    42.         {
    43.             if (Input.GetKey(KeyCode.A))
    44.             {
    45.                 transform.Translate(Vector2.left * 15f * Time.deltaTime);
    46.                 RpcLeft();
    47.                 CmdLeft();
    48.                
    49.             }
    50.             if (Input.GetKey(KeyCode.D))
    51.             {
    52.                 transform.Translate(Vector2.right * 15f * Time.deltaTime);
    53.                 RpcRight();
    54.                 CmdRight();
    55.             }
    56.             if (Input.GetKeyDown(KeyCode.Space) && coolDownTimer == 0)
    57.             {
    58.                 transform.Translate(Vector2.up * 150 * Time.deltaTime);
    59.                 coolDownTimer = coolDown;
    60.             }
    61.         }
    62.     }
    63.     [Command]
    64.     public void CmdLeft ()
    65.     {
    66.         Joust1.SetActive(true);
    67.         Joust2.SetActive(false);
    68.     }
    69.     [Command]
    70.     public void CmdRight()
    71.     {
    72.         Joust1.SetActive(false);
    73.         Joust2.SetActive(true);
    74.     }
    75.     [ClientRpc]
    76.     public void RpcLeft()
    77.     {
    78.         if (!isServer)
    79.         {
    80.             Joust1.SetActive(true);
    81.             Joust2.SetActive(false);
    82.         }
    83.     }
    84.     [ClientRpc]
    85.     public void RpcRight()
    86.     {
    87.         if (!isServer)
    88.         {
    89.             Joust1.SetActive(false);
    90.             Joust2.SetActive(true);
    91.         }
    92.     }
    93. }
    94.  
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    The clients can't send Rpc's. Only Commands. So just make sure to call the Rpc in the command.