How to instantiate the joining users to specific position?

In the sample scene DemoVoice-push to talk. How to instantiate the joining users to specific position.

Best Answers

  • CapPotatoCapPotato
    edited May 3 Accepted Answer
    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];
    
    With PhotonNetwork.playerList.Length - 1, you will get the last connected user. But if you want to execute this code directly on the MasterClient you can use OnPhotonPlayerConnected. 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);
    }

Answers

  • edited May 2
    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 here
  • Make sure to set a "PositonOffset" correctly in the inspector of "CharacterInstantiation" script.
  • 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 location
  • CapPotatoCapPotato
    edited May 3 Accepted Answer
    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];
    
    With PhotonNetwork.playerList.Length - 1, you will get the last connected user. But if you want to execute this code directly on the MasterClient you can use OnPhotonPlayerConnected. 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);
    }
  • Thanks for your reply I was able to do what I wanted :)
Sign In or Register to comment.