Controlling Remote Game Objects
Camilo Diaz - March 2023
Photon Engine makes easy for us to instantiate game objects (i.e avatars) over the network and manipulate their positions and rotations, so other players can observe their location and actions. However, not all the objects need to be instantiated at runtime, but you still need to interact with them affecting their transformations and/or rigid bodies.
An example of this behavior is having a networking user is moving a box from one place to another, and you need to let everybody in the networking session to know and observe who's doing this action and how the box is being affected by the user.
This guide assumes you already know the basic of using Unity, Unity XR Tool Kits, and Photon Engine. If you dont, please follow this tutorial and come back when you have your Photon environment configured.
Once you have your scene configured with Photon, let's create a Game Object that will be moved by the online users. In this example, we will add a remote cube (green) into the scene.
2. Add a rigid body component to the cube
3 . Add a photon view (indicates that this game object will send/receive events from the network ) component and photon rigid body view ( indicates the rigid body transformations will send over the network)
4. In the photon view component, change the Ownership transfer property to take over
5. Add a new script component called XRNetworkGrabInteract
6. Openg the script on your code editor and copy-paste the following code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
using Photon.Pun;
public class XRNetworkGrabINteract : XRGrabInteractable
{
private PhotonView m_View;
// Start is called before the first frame update
void Start()
{
m_View = GetComponent<PhotonView>();
}
// Update is called once per frame
void Update()
{
}
protected override void OnSelectEntered(XRBaseInteractor interactor)
{
m_View.RequestOwnership();
base.OnSelectEntered(interactor);
}
}
7. At this point any interaction within any player and the cube will be updated over the network and observable from the online user's POV.
Please refer to the official documentation for any additional information and technical support