Spawn / Sync existing game object (Player)?

zaki
zaki
Hello,

My current game is using vrtk (VR ToolKit). I have this CameraRig game object, which is the player character. For some reasons, this CameraRig need to be on scene first instead of using PhotonNetwork.Instantiate function to spawn it. I cant get this CameraRig sync across the network.

What I've tried so far:
Attempt1: I have added photon view and network object component to the CameraRig. And set the CameraRig game object as the observed component in photon view component. Result: Player1 cant see Player2 (and vice versa)

Attempt2: I created another player prefab, when joined room. I use PhotonNetwork.Instantiate to spawn the player prefab.
void OnJoinedRoom() {
	if (PhotonNetwork.isMasterClient)  NewPlayer (0);
}

[PunRPC]
void NewPlayer(int idx) {
	var trans = cameraRig.transform;
	var player = PhotonNetwork.Instantiate (playerAvatar.name, trans.position, trans.rotation, 0);
	player.name = "Player " + (idx + 1);
}
Now Player1 and Player2 can see each others. Then, in my Player prefab have this player setup script.
void Awake () {
        if (!photonView.isMine) {
            return;
        }
	GameObject g = GameObject.FindGameObjectWithTag("CameraRig");
        g.transform.parent = transform;
}
I grab the CameraRig game object and set it as a child of the new player prefab thats instantiated by using PhotonNetwork.Instantiaite function. As expected, the CameraRig and its children game objects' position and rotation do not sync.

Given my current scenario above, how should I spawn / sync the players(CameraRig) game object ?
(Again, i need to emphasize this, the CameraRig game object needs to be on scene before the game connect to Photon server)

My CameraRig game object structure is something like this:
CameraRig
-- Ragdoll
-- Left Hand Controller
-- Right Hand Controller
-- Camera (head)
-- etc

Please advise. Thanks.

Comments

  • Hello, anyone?
  • Hey, I currently don't have a solution for you, but I wanted to hop on this bandwagon because I also am experiencing this issue.

    I too am using VRTK (ironically) and am currently trying to find out if there is a way to have the Transform sync over Photon for an already existing GameObject. In my case, I only want to sync a single VRTK controller (currently).

    Did you make any progress, Zaki?
  • For what it's worth, I think this may be your solution.

    https://doc.photonengine.com/en-us/pun/current/manuals-and-demos/instantiation

    Scroll down all the way to the bottom and check out Manual Instantiation
  • tylo
    tylo
    edited August 2017
    Ok, I had to alter that a lot to get it working for my simple cube test. Here's what I did.

    
    
    public GameObject m_cubePrefab;
    
    void SpawnPlayerElsewhere()
    {
        // You must be in a Room already
    
        // Manually allocate PhotonViewID to an already existing local cube 
        int id1 = cubeInstance.viewID;
    
        PhotonView photonView = this.GetComponent<PhotonView>();
        photonView.RPC("SpawnOnNetwork", PhotonTargets.OthersBuffered, cubeInstance.transform.position, cubeInstance.transform.rotation, id1);
        
    }
    
    public PhotonView cubeInstance; //set this in the inspector 
    
    [PunRPC]
    void SpawnOnNetwork(Vector3 pos, Quaternion rot, int id1, PhotonMessageInfo info)
    {
        //Spawn a separate cube 
        GameObject newPlayer = Instantiate(m_cubePrefab, pos, rot) as GameObject;
    
        // Set player's PhotonView
        PhotonView[] nViews = newPlayer.GetComponentsInChildren<PhotonView>();
        
        // Set the cube's id to that of the client whom sent its local cube ID, this will sync them together
        nViews[0].viewID = id1;
    
        //Set this network'd cube's owner to be the client whom sent it
        nViews[0].TransferOwnership(info.sender);
    }
    
  • Hello Tylo,

    Sorry for the late reply, was really busy lately.
    Thanks for the link, i will check it out.

    Best regards,
    Zaki