Mohammad Faizan Khan Mohammad Faizan Khan - 1 month ago 19
C# Question

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, Normal and VR Mode.

I am able to manage these modes and user can participate with any mode but the problem is Normal Player(charcter controller) is unable to view VR controller(HTC vive Controllers) as these objects are becomes off on the side of character controller. So how do I view VR controller at normal player that what VR player is doing.

(What I have tried) I make an object and tried to imitiate it position and rotation with respect to VR controller but it is not working as Imitating source(VR controllers) are off (at the normal player side but working in VR mode) and their postion is not changing. How can I show the vr controller to other user

Answer

I have managed to solve this problem using these steps:

  1. Make one Gameobject for Head (simple cube) with Network Identity and network transform
  2. Make one Gameobject for Controller (simple cube) with Network Identity and network transform
  3. Make one Gameobject for 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 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!