0

I need help running functions on all clients and the host.

I tried the below code but it only worked on the host not on clients.

In this code I have made a jump up skill. When players take it, they can use it by pressing R button. It works fine in host but on clients doesn't work.

[Command]
void Cmd_ProvideJumpToserver()
{
    Rpc_JumpUp();
}
[ClientRpc]
void Rpc_JumpUp()
{
    if (JumpUp == 1)
    {
        Debug.Log("Jump Up Used");
        gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * 15f, ForceMode2D.Impulse);
        JumpUp = 0;
    }
}
kefren
  • 1,042
  • 7
  • 12
vNone
  • 1
  • 1
  • Well I guess something has to set `JumpUp ` to one. – AgentFire Aug 09 '19 at 08:03
  • Yes, when they take the skill from map, JumpUp variable gets to one nad also i try it by taking from map and giving from inspector but it doesn't work. By the way i getting the Debug.Log("Jump Up Used"); message on console. – vNone Aug 09 '19 at 08:07
  • could you show us how you set the `JumpUp` to 1? also you get the `Jump Up Used` method on the client or only the host? – derHugo Aug 09 '19 at 10:56

1 Answers1

0
public class Move : NetworkBehaviour
{
    private void Start()
    {
        if (isLocalPlayer) return;
        // Here we closed the script which is not ours
        GetComponent<Move>().enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.T))
        {
            CmdComand();
        }
    }

    [Command]
    private void CmdComand()
    {
        // We give it to server
        RpcToClients();
    }

    [ClientRpc]
    private void RpcToClients()
    {
        // Here we do what we want to happen on all player
        GetComponent<Rigidbody2D>().AddForce(Vector2.up * 5,ForceMode2D.Impulse);
    }
}

Thanks guys who try to help me :))

vNone
  • 1
  • 1