Search Unity

Third Party Spawning in different spawn locations in photon

Discussion in 'Multiplayer' started by GiuseppeGela, Feb 1, 2016.

  1. GiuseppeGela

    GiuseppeGela

    Joined:
    Aug 10, 2015
    Posts:
    18
    Hello everyone, I've been struggling with this for the last weekend, I've been doing a lot of research across the network and I can't find a true solution to my problem. I'm making a multiplayer game for android using photon - I have to say, it's my first attempt of making a networked game. I have the connection working fine and the players spawning fine as well, but they all spawn in the same spawn point. Now, to fix that problem, I made ad array of Transforms that contains 4 spawnpoints. When I instantiate the player I use the Random.Range method to randomly pick one of the 4 spawnpoints. But this is not enough because there will always be 1 out of 4 chances that the players spawn in the same location. I'd like to implement a function that allows me to spawn players in round (i.e, first player to spawn will be in location 1, second player in location 2 and so on). In unet this exists, it's called the RoundRobin method, but apparently this doesn't exists in photon...any suggestion? You can find the code I'm using below, what I was trying to do was using an int variable "positionNumber" that when is 0 the player is spawned in location 1 and then the variable is incremented to 1 and "stored" in the network via rpc so that the next player that logs in will see that positionNumber is equal to 1 so it will go to location 2...But I'm not sure I'm using the rpc correctly.
    Thank you very much even just for reading all of this.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using Photon;
    4. using System.Collections.Generic;
    5.  
    6. public class NetworkManager : Photon.PunBehaviour
    7. {
    8.  
    9.     const string VERSION = "v0.0.1";
    10.     public string roomName = "Maze";
    11.     public string playerPrefabName = "Player";
    12.     [SerializeField]
    13.     private Transform[] spawnPoint;
    14.     bool enableRpC = false;
    15.     int positionNumber;
    16.     int newPos;
    17.  
    18.     // Use this for initialization
    19.     void Start()
    20.     {
    21.         PhotonNetwork.ConnectUsingSettings("v0.1");
    22.         Debug.Log("Started");
    23.  
    24.     }
    25.  
    26.  
    27.  
    28.     public override void OnJoinedLobby()
    29.     {
    30.         PhotonNetwork.JoinRandomRoom();
    31.         //Debug.Log("LobbyOk");
    32.     }
    33.  
    34.     void OnPhotonRandomJoinFailed()
    35.     {
    36.         Debug.Log("Can't join random room!");
    37.         PhotonNetwork.CreateRoom(null);
    38.     }
    39.  
    40.     //public override void OnJoinedLobby()
    41.     //{
    42.     //    RoomOptions roomOptions = new RoomOptions() { isVisible = false, maxPlayers = 4 };
    43.     //    PhotonNetwork.JoinOrCreateRoom(roomName, roomOptions, TypedLobby.Default);
    44.     //    Debug.Log("");
    45.     //}
    46.  
    47.     public override void OnJoinedRoom()
    48.     {
    49.         enableRpC = true;
    50.         //PhotonNetwork.Instantiate("Player", spawnPoint[Random.Range(0, spawnPoint.Length)].position, spawnPoint[Random.Range(0, spawnPoint.Length)].rotation, 0);
    51.         //Debug.Log("Pos1 " + pos1 + " Pos2 " + pos2 + " Pos3 " + pos3 + " Pos4 " + pos4);
    52.         if (positionNumber == 0)
    53.         {
    54.             PhotonNetwork.Instantiate("Player", spawnPoint[0].position, spawnPoint[0].rotation, 0);
    55.             positionNumber = 1;    
    56.         }
    57.  
    58.         else if (positionNumber == 1)
    59.         {
    60.             PhotonNetwork.Instantiate("Player", spawnPoint[1].position, spawnPoint[1].rotation, 0);
    61.             positionNumber = 2;
    62.         }
    63.         else if (positionNumber == 2)
    64.         {
    65.             PhotonNetwork.Instantiate("Player", spawnPoint[2].position, spawnPoint[2].rotation, 0);
    66.             positionNumber = 3;
    67.         }
    68.         else if (positionNumber == 3)
    69.         {
    70.             PhotonNetwork.Instantiate("Player", spawnPoint[3].position, spawnPoint[3].rotation, 0);
    71.             positionNumber = 0;
    72.         }
    73.     }
    74.  
    75.  
    76.  
    77.  
    78.     void OnGUI()
    79.     {
    80.         GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
    81.     }
    82.  
    83.     void Update()
    84.     {
    85.         Debug.Log("new "+newPos);
    86.         if (enableRpC == true)
    87.         {
    88.             if (positionNumber == 1)
    89.             {
    90.                 photonView.RPC("ChangePos", PhotonTargets.AllBuffered, positionNumber);
    91.                 Debug.Log("rpcSent");
    92.             }
    93.              
    94.         }
    95.         Debug.Log(positionNumber);
    96.     }
    97.     [PunRPC]
    98.     void ChangePos(int pos)
    99.     {
    100.         newPos = pos;
    101.  
    102.     }
    103.  
    104.  
    105.  
    106. }
     

    Attached Files:

    tech-next and hopetolive like this.
  2. GiuseppeGela

    GiuseppeGela

    Joined:
    Aug 10, 2015
    Posts:
    18
    Weeell, I don't know if no one replied because no one knew the solution or because it was a silly question...But anyway I I found a solution to my problem by myself and I feel like sharing :)

    It might not be the best solution but it works for me:
    I created a function that checks the number of players in the room as soon as the player connects. Depending on the number of the players already in the room the new player will be spawned in the latest spawn point available, which means that if the number of players in the room is 3, then the new player will go to spawn point 4. The fifth player will restart the cycle spawning in spawn point 1, player 6 in spawn point 2 and so on.

    here's the code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using Photon;
    4. using System.Collections.Generic;
    5. using UnityEngine.UI;
    6.  
    7. public class NetworkManager : Photon.PunBehaviour
    8. {
    9.     [SerializeField]
    10.     Camera standbyCamera;
    11.     [SerializeField]
    12.     private Transform[] spawnPoint;
    13.     [SerializeField]
    14.     private GameObject ConnectButtonGO;
    15.     [SerializeField]
    16.     private Text ConnectingText;
    17.  
    18.     public GameObject Sticks;
    19.     const string VERSION = "v0.0.1";
    20.     public string roomName = "Maze";
    21.     public string playerPrefabName = "Player";
    22.     int positionNumber;
    23.     int newPos;
    24.     bool StartGame = false;
    25.     int numberPlayers;
    26.  
    27.     // Use this for initialization
    28.     void Start()
    29.     {
    30.         PhotonNetwork.ConnectUsingSettings("v0.1");
    31.         Debug.Log("1");
    32.         ConnectingText.text = "Starting connection...";
    33.     }
    34.  
    35.  
    36.  
    37.     public override void OnJoinedLobby()
    38.     {
    39.         PhotonNetwork.JoinRandomRoom();
    40.         //Debug.Log("ok");
    41.         ConnectingText.text = "Lobby Joined...Creating Room";
    42.     }
    43.  
    44.     void OnPhotonRandomJoinFailed()
    45.     {
    46.         Debug.Log("Can't join random room!");
    47.         PhotonNetwork.CreateRoom(null);
    48.     }
    49.  
    50.     //public override void OnJoinedLobby()
    51.     //{
    52.     //    RoomOptions roomOptions = new RoomOptions() { isVisible = false, maxPlayers = 4 };
    53.     //    PhotonNetwork.JoinOrCreateRoom(roomName, roomOptions, TypedLobby.Default);
    54.     //    Debug.Log("2");
    55.     //}
    56.  
    57.     public override void OnJoinedRoom()
    58.     {
    59.         ConnectingText.text = "Room joined! Press start!";
    60.         ConnectButtonGO.SetActive(true);
    61.         //PhotonNetwork.Instantiate("Player", spawnPoint[Random.Range(0, spawnPoint.Length)].position, spawnPoint[Random.Range(0, spawnPoint.Length)].rotation, 0);
    62.         //Debug.Log("Pos1 " + pos1 + " Pos2 " + pos2 + " Pos3 " + pos3 + " Pos4 " + pos4);
    63.  
    64.     }
    65.  
    66.     void OnGUI()
    67.     {
    68.         GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
    69.     }
    70.  
    71.     void Update()
    72.     {
    73.         StartGame = ConnectButton.Connect;
    74.         if (StartGame)
    75.         {
    76.             ConnectingText.enabled = false;
    77.             //Deactivate the Connection button
    78.             ConnectButtonGO.SetActive(false);
    79.             //Run CheckPlayer function to determine how many players there are in the room already
    80.             CheckPlayers();
    81.             standbyCamera.enabled = false;
    82.             //Debug.Log("Players in the room after start pressed " + PhotonNetwork.countOfPlayers.ToString());
    83.  
    84.             //Determine which spawn point to use based on the number of player
    85.             if (numberPlayers == 1)
    86.             {
    87.                 PhotonNetwork.Instantiate("Player", spawnPoint[0].position, spawnPoint[0].rotation, 0);
    88.                 numberPlayers = 2;
    89.                 ConnectButton.Connect = false;
    90.             }
    91.  
    92.             else if (numberPlayers == 2)
    93.             {
    94.                 PhotonNetwork.Instantiate("Player", spawnPoint[1].position, spawnPoint[1].rotation, 0);
    95.                 numberPlayers = 3;
    96.                 ConnectButton.Connect = false;
    97.             }
    98.             else if (numberPlayers == 3)
    99.             {
    100.                 PhotonNetwork.Instantiate("Player", spawnPoint[2].position, spawnPoint[2].rotation, 0);
    101.                 numberPlayers = 4;
    102.                 ConnectButton.Connect = false;
    103.             }
    104.             else if (numberPlayers == 4)
    105.             {
    106.                 PhotonNetwork.Instantiate("Player", spawnPoint[3].position, spawnPoint[3].rotation, 0);
    107.                 numberPlayers = 1;
    108.                 ConnectButton.Connect = false;
    109.             }
    110.         }
    111.     }
    112.  
    113.     void CheckPlayers()
    114.     {
    115.         numberPlayers = PhotonNetwork.countOfPlayers;
    116.         //if the number of player is heigher than the number of spawnpoint in the game (in this case 4),
    117.         //spawn the players in round order
    118.         for (int i = 0; i <= numberPlayers; i++)
    119.         {
    120.             if (numberPlayers > 4)
    121.             {
    122.                 numberPlayers -= 4;
    123.             }
    124.            
    125.         }
    126.     }
    127.  
     
  3. maximalniq

    maximalniq

    Joined:
    Feb 25, 2014
    Posts:
    46
    Thank you for sharing the solution ! I was searching for the same thing .
     
    GiuseppeGela likes this.
  4. XiLiT

    XiLiT

    Joined:
    Jul 1, 2016
    Posts:
    1
    THX! it work for me! :D
     
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,070
    If this solution works for you, it's totally fine :)

    I missed this post earlier, sorry. Now, I could also point everyone to the "ColorPerPlayer" script, which is now part of the PUN demos. We pick colors for each player, out of the available, editable range. The same principle could be applied to spawn points, of course.
     
  6. DA_ATeam

    DA_ATeam

    Joined:
    Dec 6, 2012
    Posts:
    5
    I also find this post to be almost the perfect solution. I've been searching for weeks, maybe a month or two. Almost perfect because I get the following error ....

    Assets/DLink.cs(108,33): error CS0103: The name `ConnectButton' does not exist in the current context

    I've built a button in the UI system and named it ConnectButton, and the error persists. I tried changing the private
    variable to a public so I could drag and drop the UI component, but noooooooooo. The error prevented the script compiling. I have a game object with my button function that I named ConnectButton, but when I drag that onto the
    button script wizard component function thing it doesn't give access to that scripts function. More than a year later,
    my question is how to I create a ConnectButton and get it to connect to this essential Class? The script is 99.9%
    just as you created it, except I had to change the name because I believe it conflicted with the built in UNET Game Manager script.

    Thanks in advance.
     
    Ivanooo likes this.
  7. LivingUniverse

    LivingUniverse

    Joined:
    Feb 18, 2013
    Posts:
    9
    I know you posted this 1 year ago and you are past this now but for others that are looking for a solution and get to this page; I'm not sure why you have all those "if else" statements when you can just do this -

    [PunRPC]
    private void SpawnPlayer(){
    spawnPoints = GameObject.FindGameObjectsWithTag("pSpawnP");
    int numberofP = PhotonNetwork.playerList.Length;
    PhotonNetwork.Instantiate(playerPrefabE.name, spawnPoints[numberofP - 1].transform.position, spawnPoints[numberofP - 1].transform.rotation, 0);
    }

    Also you should use PhotonNetwork.playerList.Length instead of countOfPlayers, which i believe is all players connected to the game.

    But dont use this method! because if you have two players spawn in and the first person leaves there will be one person left in the second spawn but if another player comes in the count will be two and the second person will spawn in the second spawn point which is occupied so they will spawn on top of each other.
     
    Last edited: Jan 22, 2017
  8. Kumar_saki

    Kumar_saki

    Joined:
    Sep 27, 2016
    Posts:
    2
    hI THANKS FOR THE SOLUTION, I was searching for this. :)
     
  9. Nikolasio

    Nikolasio

    Joined:
    Dec 6, 2014
    Posts:
    59

    Hi @tobiass,

    In which demo exactly?

    Grtz
    Nikola
     
  10. Nikolasio

    Nikolasio

    Joined:
    Dec 6, 2014
    Posts:
    59

    Found it,

    In the DemoBoxes!
     
  11. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Ciao Giuseppe non so se risponderai visto quanto tempo è passato ma ho copiato il tuo script ed oggi crea un errore in visual studio con la voce ConnectButton. Io uso Unity 2018.2
     
    Last edited: Mar 14, 2019
  12. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Has anyone found a solution to the error with Unity 2018?
     
  13. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,070
    Guiseppe shared his solution, right?
    If you don't have the exact same issue or if the solution doesn't apply to your game, please open a new post with details.
     
  14. QashiCheema

    QashiCheema

    Joined:
    May 18, 2015
    Posts:
    5
    Code (CSharp):
    1. SpawnPoints [0] = GameObject.Find ("LevelSpawnRed");
    2.         SpawnPoints [1] = GameObject.Find ("LevelSpawnGreen");
    3.         SpawnPoints [2] = GameObject.Find ("LevelSpawnBlue");
    4.         SpawnPoints [3] = GameObject.Find ("LevelSpawnYellow");
    5.  
    6.         if (PhotonNetwork.player.ID == 1)
    7.             obj = PhotonNetwork.Instantiate (Path.Combine ("Prefabs", "Tank"), SpawnPoints [0].transform.position, Quaternion.identity, 0);
    8.         else if (PhotonNetwork.player.ID == 2)
    9.             obj = PhotonNetwork.Instantiate (Path.Combine ("Prefabs", "Tank"), SpawnPoints [1].transform.position, Quaternion.identity, 0);
    10.         else if (PhotonNetwork.player.ID == 3)
    11.             obj = PhotonNetwork.Instantiate (Path.Combine ("Prefabs", "Tank"), SpawnPoints [2].transform.position, Quaternion.identity, 0);
    12.         else if (PhotonNetwork.player.ID == 4)
    13.             obj = PhotonNetwork.Instantiate (Path.Combine ("Prefabs", "Tank"), SpawnPoints [3].transform.position, Quaternion.identity, 0);
     
  15. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Hi QashiCheema the script you posted would be an addition to the script that is at the top of the post? if yes, where should it be added? Thank's
     
  16. Ivanooo

    Ivanooo

    Joined:
    Jun 19, 2019
    Posts:
    1
    For those who get the error code for [......: error CS0103: The name `ConnectButton' does not exist in the current context] here is a correction for this code to suit your pure script





    Code (CSharp):
    1.  void Update()
    2.     {
    3.        // StartGame = ConnectButton.Connect;
    4.  
    5.  
    6.  
    7.  
    8.         if (StartGame)
    9.         {
    10.  
    11.             ConnectingText.enabled = false;
    12.             //Deactivate the Connection button
    13.             ConnectButtonGO.SetActive(false);
    14.             //Run CheckPlayer function to determine how many players there are in the room already
    15.             CheckPlayers();
    16.             standbyCamera.enabled = false;
    17.             //Debug.Log("Players in the room after start pressed " + PhotonNetwork.countOfPlayers.ToString());
    18.             //Determine which spawn point to use based on the number of player
    19.             if (numberPlayers == 1)
    20.             {
    21.                 PhotonNetwork.Instantiate("Audi", spawnPoint[0].position, spawnPoint[0].rotation, 0);
    22.                 numberPlayers = 2;
    23.                 StartGame = false ;
    24.                 //ConnectButton.Connect = false;
    25.             }
    26.             else if (numberPlayers == 2)
    27.             {
    28.                 PhotonNetwork.Instantiate("Audi", spawnPoint[1].position, spawnPoint[1].rotation, 0);
    29.                 numberPlayers = 3;
    30.                 //ConnectButton.Connect = false;
    31.                     StartGame = false ;
    32.             }
    33.             else if (numberPlayers == 3)
    34.             {
    35.                 PhotonNetwork.Instantiate("Audi", spawnPoint[2].position, spawnPoint[2].rotation, 0);
    36.                 numberPlayers = 4;
    37.                 //ConnectButton.Connect = false;
    38.                     StartGame = false ;
    39.             }
    40.             else if (numberPlayers == 4)
    41.             {
    42.                 PhotonNetwork.Instantiate("Audi", spawnPoint[3].position, spawnPoint[3].rotation, 0);
    43.                 numberPlayers = 1;
    44.                 //ConnectButton.Connect = false;
    45.                     StartGame = false ;
    46.             }
    47.         }
    48.     }
     
  17. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Hi Ivanooo thanks for the reply. I want to ask you I only need the part of spawpoint because I would use the script with Realistic Car Controller. In your opinion how can I modify the script? Unfortunately I don't know much about scripts. Thank you
     
  18. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    'Don't know much about scripts', making a multiplayer game. Not a great combination in my opinion.

    I'd suggest you spend some time getting more familiar with Unity, C# and coding in general, and then when you've finished a couple or more single player games revisit your idea for a multiplayer game.
     
  19. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    81
    Hi Munchy2007 you are right and in fact a couple of single player games I managed to develop them thanks to the help of some scripts that I found on the net and that I was able to adapt. Mine for Unity is a beautiful passion but with scripts I am denied and so I was looking for someone who has passion and desire to help me with simple scripts, I don't need to do things that are too complex. Thank's
     
  20. dato818

    dato818

    Joined:
    Dec 8, 2019
    Posts:
    3
    For me non of the above solutions worked. That ColorPerPlayer script has alot of errors.

    same with using PhotonNetwork.player.ID

    Finally, created playerpref scrips in waitingroom by which assigned each player with numbers. So each time +1 player enters room playerprefs increases by one. Those number were perfect for creating spawn points in playing level
     
  21. Zshapedbanana

    Zshapedbanana

    Joined:
    Mar 20, 2016
    Posts:
    1
    Code (CSharp):
    1.         PhotonNetwork.Instantiate("NetworkPlayerController", _spawnPoint[PhotonNetwork.LocalPlayer.ActorNumber - 1].transform.position, _spawnPoint[PhotonNetwork.LocalPlayer.ActorNumber - 1].transform.rotation);
    2.  
     
    diegomh7 likes this.
  22. Alp21

    Alp21

    Joined:
    Sep 11, 2020
    Posts:
    1
    Thank you. I did not try but I think it will work :)
     
  23. Sanjay112000

    Sanjay112000

    Joined:
    Nov 9, 2020
    Posts:
    10
    Thank you bazilbaachu(https://answers.unity.com/users/1127669/bazilbaachu.html).
    I am posting my entire workflow of instantiating players at different positions:
    Initially checking whether we are master client or not:
    Then obtain list of players and send rpc to each of them individually on positions to spawn them.You can store an array of positions and send index in rpc.
    Here is the code:
    photonView = PhotonView.Get(this);
    if(PhotonNetwork.IsMasterClient)
    {
    foreach(Player pl in PhotonNetwork.PlayerList)
    {
    //Debug.Log(pl);
    photonView.RPC("InstantiationPlayer",pl,index);

    index++;
    }
    }
    [PunRPC]
    void InstantiationPlayer(int index)
    {

    PhotonNetwork.Instantiate(Playerprefabs[value].name,SpawnPoints[index].transform.position, Quaternion.identity, 0);
    }
    Thank you.