Search Unity

Resolved Issue with Netcode for GameObjects: Ownership and Communication Problem

Discussion in 'Multiplayer' started by eda-vfx, May 24, 2023.

  1. eda-vfx

    eda-vfx

    Joined:
    May 28, 2018
    Posts:
    4
    I would like to achieve several functionalities using Netcode for GameObjects.
    All players have an independent perspective and cannot see each other. Players can shoot projectiles. When a projectile collides with the camera boundaries, it should notify other players about its coordinates and the direction of the boundary it hit.

    The problem I'm currently facing is that when the host shoots a projectile and it collides with a boundary, other clients receive the notification.
    However, when a client's projectile collides with a boundary, it produces the following error: "Only the owner can invoke a ServerRpc that requires ownership!"
    I tried searching online for a solution, and some suggest using [ServerRpc(RequireOwnership = false)].

    Although this prevents the error, the host no longer receives the notification.
    [GitHub]
    Here are the scripts related to message communication:
    [CommunicationLogic.cs]

    And here is the script related to projectiles:
    [Projectile.cs]

    Could you please help me find a solution to this problem?Thanks
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,882
    On first impression I think the issue may be related to where the CommunicationLogic script is attached to.

    If it's a central object (exists only once for all players) then IsOwner won't work the way you expect it to because only one player can have ownership of that central object.

    The right way to decide whether to show a notification or not would be to let the server decide and send the message only to the client that needs to be notified. You can do so by determining the owner of the boundary and use that clientId in the RPCParams to direct the message to a single client.

    Preferably, you will want the server to determine collisions in the first place. In that case it's only a matter of sending a ClientRPC to a specific client.

    The way you describe it you have it implemented as if the local projectile determines the hit, and then sends a ServerRPC. This can lead to more inaccurate collision detection for other players as the message first needs to go to the server and then to the client(s), doubling the round-trip-time (on average).
     
  3. eda-vfx

    eda-vfx

    Joined:
    May 28, 2018
    Posts:
    4

    I have used the code from the following link: https://docs-multiplayer.unity3d.com/netcode/current/advanced-topics/message-system/clientrpc/ regarding "To send to one client, use ClientRpcSendParameters."

    I am able to send messages from the host to the client, but I am unable to send messages from one client to another client. How can I improve this?
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,882
    I think I've read that NGO will eventually support direct client messaging but currently requires the server relaying messages between clients.

    Example: client B wants to send message to client C
    B => MessageToClientServerRPC(long targetClientId)
    Server => RelayMessageToClientRPC(RPCParams: targetClientId)

    It's probably possible to use RPCParams directly rather than sending a separate clientId param.
     
    eda-vfx likes this.
  5. eda-vfx

    eda-vfx

    Joined:
    May 28, 2018
    Posts:
    4
    Thank you, I have successfully resolved my troubles