Search Unity

Third Party How to spawn players in different positions without overlapping using Photon?

Discussion in 'Multiplayer' started by BhsGamez, Nov 2, 2022.

  1. BhsGamez

    BhsGamez

    Joined:
    Jan 22, 2018
    Posts:
    7
    Hello,

    I am making a 2D multiplayer game using Photon where I have 4 spawn points and up to 4 players can join the game. The problem is that when all the players load the game at the same time, some of the players spawn at the same location, is there a way to remove the spawnPoints once its occupied?

    or any other way to fix this problem?

    The below script is instantiated once the main game level starts
    The spawn positions is also inside the gameobject that this script is attached to.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using Photon.Pun;
    3. using UnityEngine;
    4.  
    5. public class PlayerSpawner : MonoBehaviour
    6. {
    7.      public GameObject playerPrefab;
    8.  
    9.      public Transform[] spawnPositions;
    10.  
    11.      List<Transform> usedSpawnPositions;
    12.  
    13.      public static PlayerSpawner instance;
    14.  
    15.      private void Awake()
    16.      {
    17.           DontDestroyOnLoad(gameObject);
    18.  
    19.           if (instance != null)
    20.           {
    21.                Destroy(gameObject);
    22.           }
    23.           else
    24.           {
    25.                instance = this;
    26.           }
    27.      }
    28.  
    29.      void Start()
    30.      {
    31.           usedSpawnPositions = new List<Transform>();
    32.  
    33.           int randomNum = Random.Range(0, spawnPositions.Length);
    34.           if (!usedSpawnPositions.Contains(spawnPositions[randomNum]))
    35.           {
    36.                Transform spawnPoint = spawnPositions[randomNum];
    37.                PhotonNetwork.Instantiate(playerPrefab.name, spawnPoint.position, Quaternion.identity, 0);
    38.                usedSpawnPositions.Add(spawnPositions[randomNum]);
    39.           }
    40.      }
    I also tried this solution in the start function but it didn't worked either...
    Code (CSharp):
    1. GameObject[] objs = GameObject.FindGameObjectsWithTag("Player");
    2.  
    3.           if (objs.Length == 2)
    4.           {
    5.                objs[0].transform.position = spawnPositions[0].position;
    6.                objs[1].transform.position = spawnPositions[1].position;
    7.           }
    8.           if (objs.Length == 3)
    9.           {
    10.                objs[0].transform.position = spawnPositions[0].position;
    11.                objs[1].transform.position = spawnPositions[1].position;
    12.                objs[2].transform.position = spawnPositions[2].position;
    13.           }
    14.           if (objs.Length == 4)
    15.           {
    16.                objs[0].transform.position = spawnPositions[0].position;
    17.                objs[1].transform.position = spawnPositions[1].position;
    18.                objs[2].transform.position = spawnPositions[2].position;
    19.                objs[3].transform.position = spawnPositions[3].position;
    20.           }
     
  2. Strafe_m

    Strafe_m

    Joined:
    Oct 24, 2022
    Posts:
    72
    I would just use the players actor number to spawn them, i.e, Actor number with 1 will spawn at spawnPosition[0], actor number with 2 at spawnPosition[1] etc
     
    akasharma1 likes this.
  3. BhsGamez

    BhsGamez

    Joined:
    Jan 22, 2018
    Posts:
    7
    i never used actornumber can u tell me how can i call it?
     
  4. Strafe_m

    Strafe_m

    Joined:
    Oct 24, 2022
    Posts:
    72
    Code (CSharp):
    1. PhotonNetwork.LocalPlayer.ActorNumber
     
  5. BhsGamez

    BhsGamez

    Joined:
    Jan 22, 2018
    Posts:
    7
    this just shows the actor number, how can I transform its position? for example:
    Code (CSharp):
    1. if(PhotonNetwork.LocalPlayer.ActorNumber == 1)
    2.           {
    3.                //transform actor 1 position to spawnPos 1
    4.           }
     
  6. Strafe_m

    Strafe_m

    Joined:
    Oct 24, 2022
    Posts:
    72
    Code (CSharp):
    1. public Transform[] spawnPoints;
    2.  
    3. PhotonNetwork.Instantiate(playerPrefab.name, spawnPoint[PhotonNetwork.LocalPlayer.ActorNumber].position, Quaternion.identity, 0);
     
    BhsGamez likes this.
  7. dliaefimia

    dliaefimia

    Joined:
    Feb 13, 2023
    Posts:
    1
    thousands "thanks", my friend! helped me a lot!
     
  8. Rocketeer-Cis

    Rocketeer-Cis

    Joined:
    Apr 5, 2022
    Posts:
    3
    This is in some cases a good option. But when player 2 leaves and a new one comes in, he gets not that empty seat from player 2. How can I fix that?