Unity XR Interaction Toolkit and Photon 2
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Unity XR Interaction Toolkit and Photon 2
jamesmunro2
2020-08-07 18:48:36
I have been trying to use XR Toolkit and Photon 2 Networking to create a multiplayer experience. When Player A grabs an object it looks fine. Player B, watching that interaction, sees the object that Player A grabbed fighting against gravity/kinestic. I have tried multiple ways to fix this but have not succeeded. Any ideas on a solution for this? Thank you in advance.
Comments
MarshallVisions
2021-03-26 23:20:16
Here's my script for networked interactables:
public class NetworkedInteractable : MonoBehaviourPunCallbacks, IPunOwnershipCallbacks
{
internal XRBaseInteractable m_InteractableBase;
internal Player owner;
internal Rigidbody rigidBody;
internal XRController interactingController;
private bool isBeingHeld;
public UnityEvent RpcActivate;
public UnityEvent RpcDeactivate;
public UnityEvent RpcSelect;
public bool useGravity = true;
public UnityEvent RpcDeselect;
// Start is called before the first frame update
void Awake()
{
m_InteractableBase = GetComponent<XRBaseInteractable>();
rigidBody = GetComponent<Rigidbody>();
m_InteractableBase.onSelectEntered.AddListener(OnSelectEnter);
m_InteractableBase.onSelectExited.AddListener(OnSelectExit);
m_InteractableBase.onActivate.AddListener(OnActivate);
m_InteractableBase.onDeactivate.AddListener(OnDeactivate);
}
void Update()
{
if (isBeingHeld)
{
if (rigidBody)
rigidBody.isKinematic = true;
gameObject.layer = LayerMask.NameToLayer("InHand");
}
else
{
if (rigidBody && useGravity)
rigidBody.isKinematic = false;
gameObject.layer = LayerMask.NameToLayer("Interactable");
}
}
void TransferOwnership()
{
photonView.RequestOwnership();
}
public void OnSelectEnter(XRBaseInteractor obj)
{
print("Interactor: " + obj.name);
interactingController = obj.GetComponent<XRController>();
print("NetworkedInteractable -> OnSelectEnter");
if (photonView && PhotonNetwork.InRoom)
{
photonView.RPC("RpcOnSelect", RpcTarget.AllBuffered);
if (photonView.Owner != PhotonNetwork.LocalPlayer)
{
TransferOwnership();
}
}
else
{
RpcOnSelect();
}
}
public void OnSelectExit(XRBaseInteractor obj)
{
if (photonView && PhotonNetwork.InRoom)
{
photonView.RPC("RpcOnDeselect", RpcTarget.AllBuffered);
}
else
{
RpcOnDeselect();
}
interactingController = null;
}
public void OnDeactivate(XRBaseInteractor obj)
{
if (photonView && PhotonNetwork.InRoom)
photonView.RPC("RpcOnDeactivate", RpcTarget.AllBuffered);
else
RpcOnDeactivate();
}
public void OnActivate(XRBaseInteractor obj)
{
if (photonView && PhotonNetwork.InRoom)
photonView.RPC("RpcOnActivate", RpcTarget.AllBuffered);
else
RpcOnActivate();
}
[PunRPC]
public void RpcOnSelect()
{
isBeingHeld = true;
RpcSelect.Invoke();
}
[PunRPC]
public void RpcOnDeselect()
{
isBeingHeld = false;
print("RPC Deselect");
RpcDeselect.Invoke();
}
[PunRPC]
public void RpcOnActivate()
{
RpcActivate.Invoke();
}
[PunRPC]
public void RpcOnDeactivate()
{
RpcDeactivate.Invoke();
}
public void OnOwnershipRequest(PhotonView targetView, Player requestingPlayer)
{
if (targetView != photonView)
{
return;
}
print("Ownership Requested for: " + targetView.name + " from " + requestingPlayer.NickName);
photonView.TransferOwnership(requestingPlayer);
owner = requestingPlayer;
}
public void OnOwnershipTransfered(PhotonView targetView, Player previousOwner)
{
print("Ownership Transfered. New Owner: " + targetView.Owner.NickName);
}
}
Back to top