Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Distribute the message to all clients except the sender

Discussion in 'Netcode for GameObjects' started by kukimoto0425, Mar 25, 2023.

  1. kukimoto0425

    kukimoto0425

    Joined:
    Nov 20, 2017
    Posts:
    12
    How to share the coordinates of an object moved using NetCode with all clients using RPC?

    The gameObject attached with NetworkTransfer and NetworkObject components.

    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         if (IsClient)
    4.         {
    5.             sendMsgClientRpc(this.transform.position, this.transform.rotation);
    6.         }
    7.     }
    8.  
    9.  
    10.     [ServerRpc(RequireOwnership = false)]
    11.     void sendToServerRpc(Vector3 pos, Quaternion rot, ServerRpcParams serverRpcParams = default)
    12.     {
    13.  
    14.         GetComponent<Transform>().position = pos;
    15.         GetComponent<Transform>().rotation = rot;
    16.  
    17.     }
    18.  
    1) Client 1 moves the object.
    2) ServerRPC receives the coordinates.
    3) All clients' gameObjects are moved.

    In this way, the gameObject of Client1 (sende) is called again and a message is sent to ServerRPC.

    Thus, congestion occurs between ServerRpc and Client1.


    Is it better not to use RPC in this method?
    Is there any other optimal way?
     
    Last edited: Mar 25, 2023
  2. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    Isn't the network transform component supposed to handle this?
    You can send a clientRPC to update the position of the object for each client and use RPC params to decide which clients gets the message.
     
  3. kukimoto0425

    kukimoto0425

    Joined:
    Nov 20, 2017
    Posts:
    12
    Isn't ClientRPC only called from the Server?
    Are there Client-to-Clinet messeaging method?

    I tried to find ClientRpc parameter reference, but I can not find.
    Would you let me know the URL which is described about ClientRpc parameter.
     
  4. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
  5. kukimoto0425

    kukimoto0425

    Joined:
    Nov 20, 2017
    Posts:
    12
    I changed the code
    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         if (IsClient)
    4.         {
    5.             sendToServerRpc(this.transform.position, this.transform.rotation);
    6.         }
    7.     }
    8.  
    9.  
    10.     [ServerRpc(RequireOwnership = false)]
    11.     void sendToServerRpc(Vector3 pos, Quaternion rot, ServerRpcParams serverRpcParams = default)
    12.     {
    13.         sendMsgClientRpc(pos, rot);
    14.     }
    15.  
    16.     [ClientRpc]
    17.     void sendMsgClientRpc(Vector3 pos, Quaternion rot, ClientRpcParams clientRpcParans = default)
    18.     {
    19.         GetComponent<Transform>().position = pos;
    20.         GetComponent<Transform>().rotation = rot;
    21.     }
    22.  
    Indeed, I could call ClinetRPC from ServerRPC.
    However, changing the position of an object in a Client does not change the position of objects in other Clients.

    Is the Network Trasfer component affecting this?

    Code (CSharp):
    1. void sendMsgClientRpc(Vector3 pos, Quaternion rot, ClientRpcParams clientRpcParans = default)
    In the above declaration, clientRpcParans seems to have only dedault.

    The inability to change the position of an object only from the Server may be a basic design error.

    Is there a sample that allows the object to be repositioned from the Client?
     
  6. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    You shouldn't change the position of an object like that. If you want to change it's position you need to do it fro mthe server, doing it from the clients will be possible only if the object is local and that's not a good solution. You should probaly get back to your first code. I can't think of a goos way to stop the server from sending the position to the client again.
     
  7. kukimoto0425

    kukimoto0425

    Joined:
    Nov 20, 2017
    Posts:
    12
    Thanks IavagoatGG:)