Instantiate two player in world parent ???

Options
Spincu
edited May 2014 in Any Topic & Chat
Hello guys, I really need some help on this one as I've been grinding on it for days now. I have a gameobject "GameWorld" in my scene and I want two instantiate my players in that world gameobject. The problem is The master is instantiated in the gameworld object while the other client is always instantiated outside that gameobject. I've also tried with RPC but got even worse results, like not instantiating at all or can't controlling player, players not visibile etc. Inside the world gameobject i have a gamecenter object with the network script and a photon view. The playerPreba also has a photon view on it. I really don't know what else I can try.

This is the closest I've got with anything good, seeing both players and controlling them but the second one is outside gameWorld.
void Start(){
		 SpawnPlayer();
	}
	
	void SpawnPlayer(){	
			
		if (PhotonNetwork.player.isMasterClient){		
			// we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
	        clone =  PhotonNetwork.Instantiate(this.playerPrefab.name, SpawnPoint1.position, SpawnPoint1.rotation,0);	
		}else{
			// we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
	        clone =  PhotonNetwork.Instantiate(this.playerPrefab.name, SpawnPoint2.position, SpawnPoint2.rotation,0);	
		}
		
		clone.transform.parent = gameWorld.transform;
	}



and this is with RPC: but it's not instantiating the second player anymore even though the log say he's connected and the first player prefab doesn't have owner the master but the scene.
[RPC]
	void SpawnPlayer(string worldname){
		
	    GameObject clone=null;
		
		if (PhotonNetwork.isMasterClient){		
			// we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
	        clone = (GameObject)Instantiate(playerPrefab, SpawnPoint1.position, SpawnPoint1.rotation);	
		}else if(PhotonNetwork.isNonMasterClientInRoom){
			//we're in a room. spawn a character for the local player. it gets synced by using PhotonNetwork.Instantiate
	        clone = (GameObject)Instantiate(playerPrefab, SpawnPoint2.position, SpawnPoint2.rotation);		
		}
		
		clone.transform.parent = GameObject.FindGameObjectWithTag(worldname).transform;
	}

//and calling it like so  --  SpawnPlayer("GameWorld");

I've also tried without the tag, and put in different places like awake(), start(), OnJoinedRoom(), OnPhotonPlayerConnected and still no good results.

I'm also using it like the demo worker scene, connecting in one scene and then starting the game in another.

Please help me as I need this fixed urgently and I'm exhausted and the documentation is useless with instantiating inside a parent.

Comments

  • Tobias
    Options
    Instantiate does not reflect parenting and always just puts each object in the root of the scene, afaik.
    However, whenever PUN instantiates a GO, it will also call the callback OnPhotonInstantiate on that GameObject. If you implement that in a script on the Prefab, you should be able to set the parent of this new GameObject.
    You will have to make sure your synchronization is based on the local coordinates of a GameObject, or else you will send world coordinates even though your game object has some parent.

    Why do you need the parenting?
  • Spincu
    Options
    Hello Tobias.

    I want to make a multiplayer augmented reality game. For that I need the have a single gameworld object to associate with the marker. The whole game has to be inside that gameobject.

    So you're saying if I set a script on the player to find it's parent it should work ? Instead of setting it from the main script?
  • Spincu
    Options
    Hey it works like that. Not the most elegant of solutions but it will have to do for now. Thanks :D