Search Unity

Question Spawn players in different positions [Solved]

Discussion in 'Multiplayer' started by Hatemsla, Aug 7, 2022.

  1. Hatemsla

    Hatemsla

    Joined:
    Oct 17, 2021
    Posts:
    62
    I'm trying to spawn players in different positions. I use RPC for this, the players will spawn in different positions, as I want, BUT this method additionally duplicates (to be more precise, it will spawn copies of the player depending on the total number of all players) of the player, how can this be fixed? Maybe there is some kind of test for this? Or a better way?
    Code (CSharp):
    1. public class PlayerManager : MonoBehaviour
    2. {
    3.     private PhotonView _photonView;
    4.  
    5.     private void Start()
    6.     {
    7.         _photonView = GetComponent<PhotonView>();
    8.  
    9.         if (_photonView.IsMine)
    10.         {
    11.             int i = 0;
    12.             foreach (var p in PhotonNetwork.PlayerList)
    13.             {
    14.                 _photonView.RPC(nameof(CreateController), p, i);
    15.                 i++;
    16.             }
    17.         }
    18.     }
    19.  
    20.     [PunRPC]
    21.     private void CreateController(int i)
    22.     {
    23.         var playerAvatar = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Player"), GameManager.instance.spawnPoints[i].position, Quaternion.identity);
    24.     }
    25. }
    If you add break after i++, then only the Master player and his copies will be saved.
     
  2. silverleecams

    silverleecams

    Joined:
    May 12, 2022
    Posts:
    51
    Following because I'm trying to make a respawn system too
     
  3. Hatemsla

    Hatemsla

    Joined:
    Oct 17, 2021
    Posts:
    62
    Anyone who needs a solution.
    Code (CSharp):
    1. public class PlayerManager : MonoBehaviour
    2. {
    3.     private PhotonView _photonView;
    4.     private void Start()
    5.     {
    6.         _photonView = GetComponent<PhotonView>();
    7.  
    8.         if (_photonView.IsMine)
    9.         {
    10.             int i = Array.IndexOf(PhotonNetwork.PlayerList, PhotonNetwork.LocalPlayer);
    11.             var playerAvatar = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Player"),
    12.                 GameManager.instance.spawnPoints[i].transform.position, Quaternion.identity);
    13.         }
    14.     }
    15. }