Search Unity

Spawning in different spawn locations in photon

Discussion in 'Connected Games' started by GiuseppeGela, Feb 1, 2016.

  1. GiuseppeGela

    GiuseppeGela

    Joined:
    Aug 10, 2015
    Posts:
    8
    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:

  2. GiuseppeGela

    GiuseppeGela

    Joined:
    Aug 10, 2015
    Posts:
    8
    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.  
     
    tobiass and XiLiT like this.
  3. maximalniq

    maximalniq

    Joined:
    Feb 25, 2014
    Posts:
    43
    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:
    2,125
    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:
    4
    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.
     
  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:
    45

    Hi @tobiass,

    In which demo exactly?

    Grtz
    Nikola
     
  10. Nikolasio

    Nikolasio

    Joined:
    Dec 6, 2014
    Posts:
    45

    Found it,

    In the DemoBoxes!
     
  11. Silveralby

    Silveralby

    Joined:
    Jan 14, 2018
    Posts:
    38
    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 at 10:12 AM
  12. Silveralby

    Silveralby

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

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,125
    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.