Search code examples
c#unity-game-enginevirtual-realityunity3d-unethtc-vive

VR Player controller is not showing to character controller


I am developing a Multiplayer game where a Normal Character Controller User and VR User can participate. So there are two modes of the games through a user can join,

  1. Normal Mode (Character Controller)
  2. VR Mode (HTC VR headset)

I am able to manage these modes and user can participate with any mode but the problem is Normal Player (character controller) is unable to view VR controller (HTC Vive Controllers) as these objects become turn off on the side of character controller. So how do I show VR controller to the Normal player side that what VR player is doing currently.

(What I have tried) I make an object and tried to imitate its position and rotation with respect to VR controller but it is not working as Imitating source (VR controllers) objects are off (at the normal player side but working in VR mode) and their positions is not changing. How can I show the VR controller to other users in unity3d??


Solution

  • I have managed to solve this problem using these steps after trying hard three four days. These step are given below so that future user don't get stuck in this problem as there are no other comprehensive guide available:

    1. Make one Gameobject for Head (simple cube) with Network Identity and network transform

    2. Make one Gameobject for(right) Controller (simple cube) with Network Identity and network transform

    3. Make one Gameobject for(left) other Controller (simple cube) with network Identity and network transform

    4. Make prefab of all above gameobjects.

    5. Add all three prefabs into Network Manager (registered Spawnable prefabs list)

    6. delete three prefab from scene

    7. Added below script(See comments for details) into my vr player and assign respective prefabs and gameobjects

      public class VRPlayerCtrl : NetworkTransform {
      
      //source gameobjects head, left and right controller object of htc vive prefab
      
      public GameObject rightContSource;
      
      public GameObject leftContSource;
      
      public GameObject headObjSource;
      
      
      //prefabs to assign head, left controller, and right controller
      public GameObject vrHeadObjPrefab;
      public GameObject vrLeftCtrlPrefab;
      public GameObject vrRightCtrlPrefab;
      
      GameObject vrHeadObj;
      GameObject vrLeftCtrl;
      GameObject vrRightCtrl;
      
      void Start()
      {
      
          Debug.Log("Start of the vr player");
      
          if (isLocalPlayer)
          {
              //instantiate prefabs
              CmdInstantiteHeadAndController();
              //disabled conroller meshes at VR player side so it cannont be view by him
              vrLeftCtrl.GetComponent<MeshRenderer>().enabled = false;
              vrRightCtrl.GetComponent<MeshRenderer>().enabled = false;
          }
      }
      
      //Instantiate on start head and vr controller object so that it can be view by normal players
      void CmdInstantiteHeadAndController()
      {
          Debug.Log("instantiateing the controller and head object");
          vrHeadObj = (GameObject)Instantiate(vrHeadObjPrefab);
          vrLeftCtrl = (GameObject)Instantiate(vrLeftCtrlPrefab);
          vrRightCtrl = (GameObject)Instantiate(vrRightCtrlPrefab);
      
          // spawn the bullet on the clients
          NetworkServer.Spawn(vrHeadObj);
          NetworkServer.Spawn(vrLeftCtrl);
          NetworkServer.Spawn(vrRightCtrl);
      }
      
      void Update()
      {
          if (!isLocalPlayer)
          {
              return;
          }
      
          //sync pos on network
          CmdControllerPositionSync();
      }
      //sync position on VR controller objects so that VR player movemnts/action can be viewd by normal user
      [Command]
      public void CmdControllerPositionSync()
      {
      
          vrHeadObj.transform.localRotation = headObjSource.transform.localRotation;
          vrHeadObj.transform.position = headObjSource.transform.position;
          vrLeftCtrl.transform.localRotation = leftContSource.transform.localRotation;
          vrRightCtrl.transform.localRotation = rightContSource.transform.localRotation;
          vrLeftCtrl.transform.localPosition = leftContSource.transform.position;
          vrRightCtrl.transform.localPosition = rightContSource.transform.position;
      }
      
      }
      

    congrats you have done!