Search Unity

Question How to change ownership on interactables in netcode

Discussion in 'VR' started by bwecke, Dec 21, 2023.

  1. bwecke

    bwecke

    Joined:
    Nov 16, 2023
    Posts:
    2
    Hi
    I have a netcode project with an XROrigin and a NetworkPlayer prefab. One player starts the application and automatically opens the server as a host and the second one automatically joins after opening the application. Now I want just a ball that can be picked up and held by both, this ball should be synchronized. The ball gets spawned by pressing SPACE as the host. The host can also take the ball and the 2nd client sees this synchronized. But if the client wants to take the ball it is 1. not snyced with the host (he just doesnt see it) and 2. the ball gets teleported back to the original position after deselecting it. I tried to change the ownership. I copied a method from a tutorial and bound this method to the controllers of the XROrigin when Selected on a XR Direct Interactor. This method is within the NetworkPlayer Script that lies on the NetworkPlayer prefab:

    Code (CSharp):
    1. public void OnSelectGrabbable(SelectEnterEventArgs eventArgs)
    2. {
    3.    Debug.Log("Trying to pick up for this OwnerClientId " + OwnerClientId);
    4.    Debug.Log("NetworkId " + NetworkObjectId);
    5.    Debug.Log("IsClient: " + IsClient + " IsOwner: " + IsOwner);
    6.  
    7.    if (IsClient && IsOwner)
    8.    {
    9.    NetworkObject networkObjectSelected = eventArgs.interactableObject.transform.GetComponent<NetworkObject>();
    10.    Debug.Log("This is the selected object: " + networkObjectSelected);
    11.    if (networkObjectSelected != null)
    12.    {
    13.        RequestGrabbableOwnerShipServerRpc(OwnerClientId, networkObjectSelected);
    14.    }
    15.    }
    16. }
    17.  
    18. [ServerRpc]
    19. public void RequestGrabbableOwnerShipServerRpc(ulong newOwnerClientId, NetworkObjectReference networkObjectReference)
    20. {
    21.    if (networkObjectReference.TryGet(out NetworkObject networkObject))
    22.    {
    23.        if (networkObject.OwnerClientId == newOwnerClientId)
    24.        {
    25.            return;
    26.        }
    27.        Debug.Log("This is the selected object: " + networkObject);
    28.        Debug.Log("And this the client ID: " + newOwnerClientId);
    29.        networkObject.ChangeOwnership(newOwnerClientId);
    30.    }
    31.    else
    32.    {
    33.        Debug.Log("Unable Changeownership for: " + newOwnerClientId);
    34.    }
    35. }
    There are some weird behaviours now. A normal session on a client looks like this:
    Host is hosting
    • Spawning this OwnerClientId 0
    • NetworkObjectId 1, IsClient: True, IsOwner: False
    Client is joining:
    • Spawning this OwnerClientId 1
    • NetworkObjectId 3, IsClient: True, IsOwner: True
    Then the ball is spawned with NetworkObjectId 4 and OwnerClientId 0 (spawned by host) If I now want to take the ball as a client it logs these values: OwnerClientId 0, NetworkObjectId 0 IsClient: False, IsOwner: False And I dont understand that, I didnt even found an object in the hierarchy with the NetworkObjectId 0.
    I used a tutorial from valem to setup the project and unity relay. And the interactable code is from dilmer valecilos
     
  2. amankAtUnity

    amankAtUnity

    Joined:
    Jan 27, 2019
    Posts:
    20
    Ping .. any news on this?
     
  3. amankAtUnity

    amankAtUnity

    Joined:
    Jan 27, 2019
    Posts:
    20
    Hi,
    I really appreciate your fast support. In particular - since this is a widely discussed challange I am facing e.g.:
    - https://forum.unity.com/threads/move-other-players-with-xr-grab-interactable.1479960/
    - https://forum.unity.com/threads/xri-netcode-client-grab.1492559/
    - https://forum.unity.com/threads/how-to-change-ownership-on-interactables-in-netcode.1529314/
    - https://gamedev.stackexchange.com/questions/199174/unity-xr-toolkit-and-mirror-networking-grabbing

    I am using:
    - 2022.3.16f1
    - Netcode for GameObjects 1.7.1
    - Relay 1.0.5
    - XR Interaction Toolkit 2.5.2
    - 2 Meta Quest 3

    Following the SETUP mentioned here:
    -

    -


    GOAL: Each player is able to spawn objects that can be grabbed (XR Grabble) and moved arround by both players.

    PROBLEM: If client player grabs, moves or rotates the object, it snaps back to the old position after exiting select. I'm facing the problem even if i requestchange ownership via serverRPC. Reuqesting the ownership slightly changes the behavior - meaning that very small changes of rotations seam to be transmited to the sever somehow.

    ASSUMPTION: The missmbehavior ist somehow related to the fact that the grabbed object is reparented for interaction - but I am not sure.

    In the attachement you will find a screenshot of the object. In addition I attached the changeOwnership script.

    Does it make sense?
    Dd you need any additional information? .. upload screen recordings?
    Is there any sample showing how to have a xr grabbable object being moved arround by two players with netcode for GameObjects and Relay? I means this seamas to me a very obvious XR use case.

    Your help is very appreciated! Hope to hear from you soon!

    BR,
    Andreas
     

    Attached Files:

    Bender_R likes this.
  4. Bender_R

    Bender_R

    Joined:
    May 21, 2018
    Posts:
    115
    I'm facing the same problem.

    This is happening on client:


    Is there someone who can help me with a simple example of how to do grabbing over netcode using xr grab interactable?
     
  5. dhiegolucio

    dhiegolucio

    Joined:
    Mar 2, 2016
    Posts:
    6
    Hi guys, I'm facing the same problem, but I found some light that may help. I have not try it yet, so you may try and tell if it worked or not. Anyway, let's go:
    He'll explain in the video, but basically what he did was that the server moves the cube to follow the client's hand.