How to instantiate the joining users to specific position?
Options
in Photon Voice
In the sample scene DemoVoice-push to talk. How to instantiate the joining users to specific position.
0
Best Answers
-
There are two solutions. The first one is to use that on the OnJoinedRoom() function (in your network manager): PhotonNetwork.Instantiate(MyPrefab, spawnPoint.position, spawnPoint.rotation, 0);
If you can't use that you can also make a script to apply a position on the player after it spawned. Make a condition directly on your network manager or on the Start() of your Player script on his GameObject.5 -
OnJoinedRoom()
function is only for the local player. So if you want to use that you can also update your code like this:spawnPos = spawnPositions[PhotonNetwork.playerList.Length - 1];
WithPhotonNetwork.playerList.Length - 1
, you will get the last connected user. But if you want to execute this code directly on the MasterClient you can useOnPhotonPlayerConnected
. This function will be call on the MasterClient for each new player connection in the room, but not for the MasterClient. For him you need to keep your OnJoinedRoom function.public void OnPhotonPlayerConnected(PhotonPlayer player) { Debug.Log("OnPhotonPlayerConnected: " + player); }
1
Answers
-
There are two solutions. The first one is to use that on the OnJoinedRoom() function (in your network manager): PhotonNetwork.Instantiate(MyPrefab, spawnPoint.position, spawnPoint.rotation, 0);
If you can't use that you can also make a script to apply a position on the player after it spawned. Make a condition directly on your network manager or on the Start() of your Player script on his GameObject.5 -
I tried editing the character instantiating script initialized a counter and it looks like this
public class CharacterInstantiation : OnJoinedInstantiate { public delegate void OnCharacterInstantiated(GameObject character); public static event OnCharacterInstantiated CharacterInstantiated; public int counter = 0; public Vector3[] spawnPositions; public new void OnJoinedRoom() { if (this.PrefabsToInstantiate != null) { GameObject o = PrefabsToInstantiate[(PhotonNetwork.player.ID - 1) % 4]; //Debug.Log("Instantiating: " + o.name); Vector3 spawnPos = Vector3.zero; if (this.SpawnPosition != null) { spawnPos = spawnPositions[counter]; } Vector3 random = Random.insideUnitSphere; random = this.PositionOffset * random.normalized; spawnPos += random; spawnPos.y = 0; Camera.main.transform.position += spawnPos; o = PhotonNetwork.Instantiate(o.name, spawnPos, Quaternion.identity, 0); if (CharacterInstantiated != null) { CharacterInstantiated(o); counter++; } } } }
I tried the code offline in one of the samples it worked but when in photonsettings I got online it is not working, user 1 who creates the room is spawning at correct place but others also come at user1 place. What should I do here0 -
Make sure to set a "PositonOffset" correctly in the inspector of "CharacterInstantiation" script.0
-
positionoffset I set it to 3 and it is still the same. I just want the user joining spawns to a specific location. in the character script I mention earlier it spawns the first person creating and joining the room to my set location but rest joining also spawn at user one location0
-
OnJoinedRoom()
function is only for the local player. So if you want to use that you can also update your code like this:spawnPos = spawnPositions[PhotonNetwork.playerList.Length - 1];
WithPhotonNetwork.playerList.Length - 1
, you will get the last connected user. But if you want to execute this code directly on the MasterClient you can useOnPhotonPlayerConnected
. This function will be call on the MasterClient for each new player connection in the room, but not for the MasterClient. For him you need to keep your OnJoinedRoom function.public void OnPhotonPlayerConnected(PhotonPlayer player) { Debug.Log("OnPhotonPlayerConnected: " + player); }
1 -
Thanks for your reply I was able to do what I wanted0