- Home /
Photon player instantiation BY MAGIC!?
I've got a little game I'm building, using the Photon Asteroid example as a model. that is, it has a 'lobby' where players gather, then enter the main scene. When I load the main scene I instantiate player avatars. I must be doing something wrong, but it's spooky what happens:
Player[] sorted = PhotonNetwork.PlayerList.OrderBy((p) => p.ActorNumber).ToArray();
Debug.Log(sorted.Length + " users in room");
foreach (Player p in sorted)
{
Debug.Log(string.Format("{0} {1}: is local {2}, is masterclient {3}, ", p.NickName, p.UserId, p.IsLocal, p.IsMasterClient));
if (p.IsLocal)
{
GameObject go = PhotonNetwork.Instantiate(playerPrefab.name, Camera.main.transform.position, Quaternion.identity, 0);
go.transform.parent = Camera.main.transform;
go.name = p.NickName;
go.SendMessage("SetAvatarName", p.NickName); ;
}
}
I'm assuming I'm only going to instantiate my avatar when it's 'local' (that's 'me' right?). In fact, I maybe don't even need to see 'me' so I could avoid this?
Well here's the spooky part. If I join one player from remote, I have 2 players when I enter this scene. One isLocal, the other is not. So, only ONE (the local one) should be created, yes? BUT TWO are created. And I cannot figure out where it's coming from. This is the only place in the game I reference playerPrefab, and it's in my class (unless it's hiding a same-named object in a base class???) Anyway, the code that sets the name and avatar label does NOT get executed on the remote user. Everything else about the remote player works fine, I can see him moving properly about the scene.
Any insights welcome.
What do you mean 2 are created? Is it 2 characters spawning per photon player, so 4 in total?
Because as I read the code, there are 2 photon players in the game, and each spawns a player of their own, making 2 gamer characters in the scene.
If you create a character via photonNetwork.Instantiate, it is a synched object that will be created for everyone that is in the same photon room. As stated by purpl3grape, seeing two character objects is the expected behavior of this code snippet. The masterclient enters the room and creates a character via photonnetwork. This character will be created for everyone that is in the room (and joins later). When the second player joins he will also create a character object that will be synched for everyone in the room. So you got one synched character for each player.
As said. The local character you see is created by this script. The remote (non-local) character is the copy, that photonNetwork will create for all other players in the room.