Search Unity

Third Party (PUN2/Photon) Stop players from spawning in the same spot?

Discussion in 'Multiplayer' started by Chr0my, May 26, 2020.

  1. Chr0my

    Chr0my

    Joined:
    Dec 28, 2018
    Posts:
    2
    Hi! First ever post here.

    So, this isnt an original post but https://forum.unity.com/threads/any-way-to-stop-players-spawning-in-the-same-place.362666/ Didnt really help since my players dont move, they are stationary in their spawn.

    Below is what i have as the spawning code. What would i need to add to this if i want to make sure no player spawns in the same spawn? Because currently they can/do.

    Code (CSharp):
    1.  
    2.     public void Spawn()
    3.     {
    4.         int randomNumber = Random.Range(0, spawnPoints.Length - 1);
    5.         Vector3 spawnPoint = spawnPoints[randomNumber].GetComponent<Transform>().position;
    6.         Debug.Log("Creating Player");
    7.         PhotonNetwork.Instantiate("Player", spawnPoint, Quaternion.identity, 0);
    8.     }
    9.  
    (i have Spawn(); in Start())
    Non coder here, well i can code but pretty new and only basics.
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,068
    Each connected player has a PhotonNetwork.LocalPlayer.ActorNumber which is unique.
    This could be used to place each joining player on a new spot.
    Downside: When a player leaves, the spot is empty.

    Better:
    The PlayerNumbering component will handle leaving and joining players and assign player numbers. Let's say 1 to 4 in a 4 player room / game.
    Place 4 objects in the scene as "spawn point marker" and assign numbers 1 to 4. Pretty much done.
     
    Dima_96 likes this.
  3. arvindchetu

    arvindchetu

    Joined:
    Jun 3, 2020
    Posts:
    10
    PhotonNetwork.LocalPlayer.ActorNumber is used for such this thing . This could be used to place each joining player on a new spot. Otherwise a simple integer counter can do your work on just increase that value at time of instantiating of players.