Search Unity

Third Party Call function on all clients except the sender Mirror

Discussion in 'Multiplayer' started by Vzrod, Apr 15, 2021.

  1. Vzrod

    Vzrod

    Joined:
    Apr 2, 2020
    Posts:
    2
    Hi, I'm new in multiplayer and I use Mirror. I just want when a player collides with a power block (simulate by pressing F2), it will call a function on all clients except the sender. I show you my code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Mirror;
    4. using UnityEngine;
    5.  
    6. public class PlayerPowers : NetworkBehaviour
    7. {
    8.     [SerializeField]
    9.     private GameObject cam;
    10.     public float rotationCameraTime;
    11.  
    12.     void Start()
    13.     {
    14.         cam = GameObject.FindGameObjectWithTag("PlayerCamera");
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if (Input.GetKeyDown(KeyCode.F2))
    20.         {
    21.             CmdRotationCamera(this.netId); //It send the Id of the transmitter
    22.             Debug.Log("Call Cmd : " + this.netId);
    23.         }
    24.     }
    25.  
    26.     [Command]
    27.     public void CmdRotationCamera(float clientCallCmdId)
    28.     {
    29.         Debug.Log("Id of the Cmd : " + clientCallCmdId);
    30.         ClientRotationCamera(clientCallCmdId);
    31.     }
    32.  
    33.  
    34.     [ClientRpc]
    35.     public void ClientRotationCamera(float clientCallCmdId)
    36.     {
    37.         Debug.Log("Client id : " + this.netId + "      clientcallcmdid : " + clientCallCmdId);
    38.         if (this.netId != clientCallCmdId)
    39.         {
    40.             StartCoroutine(RotationCameraTimer());
    41.         }
    42.     }
    43.  
    44.     IEnumerator RotationCameraTimer()
    45.     {
    46.         cam.transform.localRotation = Quaternion.Euler(0f, 0f, 180f);
    47.  
    48.         yield return new WaitForSeconds(rotationCameraTime);
    49.  
    50.         cam.transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
    51.     }
    But when I start the program and test it, in the void ClientRotationCamera the id of the player who starts the function (client 2) is correctly stored (clientCallCmdId = 2) but in the consol of the Client 1 the client id (this.netId) should be the id of the player of the client 1 (so 1) but it's equal to 2. I don't know what happened. Please can you help me and tell me if I don't tell you enough information.
     
  2. Vzrod

    Vzrod

    Joined:
    Apr 2, 2020
    Posts:
    2
  3. seanrich92

    seanrich92

    Joined:
    Oct 13, 2020
    Posts:
    6
    Maybe try to pass the netId as ulong. Maybe the conversion from float to ulong may cause issues?