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:
public void OnSelectGrabbable(SelectEnterEventArgs eventArgs)
{
Debug.Log("Trying to pick up for this OwnerClientId " + OwnerClientId);
Debug.Log("NetworkId " + NetworkObjectId);
Debug.Log("IsClient: " + IsClient + " IsOwner: " + IsOwner);
if (IsClient && IsOwner)
{
NetworkObject networkObjectSelected = eventArgs.interactableObject.transform.GetComponent<NetworkObject>();
Debug.Log("This is the selected object: " + networkObjectSelected);
if (networkObjectSelected != null)
{
RequestGrabbableOwnerShipServerRpc(OwnerClientId, networkObjectSelected);
}
}
}
[ServerRpc]
public void RequestGrabbableOwnerShipServerRpc(ulong newOwnerClientId, NetworkObjectReference networkObjectReference)
{
if (networkObjectReference.TryGet(out NetworkObject networkObject))
{
if (networkObject.OwnerClientId == newOwnerClientId)
{
return;
}
Debug.Log("This is the selected object: " + networkObject);
Debug.Log("And this the client ID: " + newOwnerClientId);
networkObject.ChangeOwnership(newOwnerClientId);
}
else
{
Debug.Log("Unable Changeownership for: " + newOwnerClientId);
}
}
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
