Search Unity

Networking 2 HMD's Using MLAPI

Discussion in 'Netcode for GameObjects' started by TykaTEETEE, Sep 23, 2021.

  1. TykaTEETEE

    TykaTEETEE

    Joined:
    Jun 7, 2021
    Posts:
    1
    Hi everyone!

    I've been trying to sync up 2 different oculus quests acting as clients to a server and have them both render their own separate cameras. I've created a prefab with a camera rig, network object, and network transform and can get both players to connect, however upon the second player connecting, the cameras switch. Positionally and rotationally the hmd's send the data correctly, and if there is only one client connected, it shows the proper camera. Server side it shows the hmd's pov as the correct camera as well, however in the headset it's not the same because when the second player connects, the hmd's immediately start showing each others pov, and not their own. Here is the code I am using for the movement and cameras. Any help or suggestions would be appreciated!
    Code (CSharp):
    1. using UnityEngine;
    2. using MLAPI;
    3.  
    4. [RequireComponent(typeof(CharacterController))]
    5. public class OculusMovement : NetworkBehaviour
    6. {
    7.     private readonly bool HmdRotates = true;
    8.     CharacterController Controller = null;
    9.     protected OVRCameraRig CameraRig = null;
    10.     protected Transform centerEye = null;
    11.     protected Transform root = null;
    12.     protected OVRCameraRig[] CameraRigs;
    13.  
    14.     void Start()
    15.     {
    16.         var p = CameraRig.transform.localPosition;
    17.         p.z = OVRManager.profile.eyeDepth;
    18.         CameraRig.transform.localPosition = p;
    19.         Controller = gameObject.GetComponent<CharacterController>();
    20.     }
    21.  
    22.     void Awake()
    23.     {
    24.         CameraRigs = gameObject.GetComponentsInChildren<OVRCameraRig>();
    25.         CameraRig = CameraRigs[0];
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         CameraRig.UpdatedAnchors += UpdateTransform;
    31.     }
    32.  
    33.     public void UpdateTransform(OVRCameraRig rig)
    34.     {
    35.         root = CameraRig.trackingSpace;
    36.         centerEye = CameraRig.centerEyeAnchor;
    37.  
    38.         if (HmdRotates)
    39.         {
    40.             // Transforms the HMD
    41.             Vector3 prevPos = root.position;
    42.             Quaternion prevRot = root.rotation;
    43.  
    44.             transform.SetPositionAndRotation(new Vector3(centerEye.position.x, centerEye.position.y, centerEye.position.z), Quaternion.Euler(centerEye.rotation.eulerAngles.x, centerEye.rotation.eulerAngles.y, centerEye.rotation.eulerAngles.z));
    45.  
    46.             root.SetPositionAndRotation(prevPos, prevRot);
    47.         }
    48.     }
    49. }
     
  2. jdcart

    jdcart

    Joined:
    Jun 24, 2015
    Posts:
    15
    Make sure that the OculusMovement class, any other classes and any cameras for your HMD rig are only enabled for the local player. That way when the second player connects, you don't have duplicate inputs or cameras.

    Code (CSharp):
    1. Start()
    2. {
    3.     if(!isLocalPlayer)
    4.     {
    5.          //Disable Camera and other components
    6.     }
    7. }