Search Unity

2 Payer random matchmaking

Discussion in 'Multiplayer' started by Y_Gunes, Feb 15, 2019.

  1. Y_Gunes

    Y_Gunes

    Joined:
    Apr 1, 2018
    Posts:
    5
    Hello everyone, I am developing a game using Photonnetwork. I want to do this, 2 players enter the room when the game begins. Yes the game starts but the player who created the room cannot spawn.Other player can spawn.

    Only one player participates in the game.

    I'm spawning the user in the "updateOtherPlayerStatus()" method, where am I making mistakes?

    Thanks already for your help.

    My code;

    Code (CSharp):
    1. public void Connect()
    2.     {
    3.         car[CarSelected].carObj.GetComponent<RCC_PhotonNetwork>().enabled = true;
    4.         PhotonNetwork.automaticallySyncScene = true;
    5.  
    6.         PhotonNetwork.PhotonServerSettings.HostType = ServerSettings.HostingOption.BestRegion;
    7.         PhotonNetwork.ConnectToBestCloudServer(appVersion);
    8.     }
    9.  
    10.     public override void OnFailedToConnectToPhoton(DisconnectCause cause)
    11.     {
    12.         if (connectionFailedEvent != null)
    13.             connectionFailedEvent();
    14.     }
    15.  
    16.     private IEnumerator requestJoinRandomRoom()
    17.     {
    18.         while (!PhotonNetwork.connectedAndReady)
    19.         {
    20.             yield return new WaitForEndOfFrame();
    21.         };
    22.         PhotonNetwork.JoinRandomRoom();
    23.     }
    24.  
    25.     public void StartMatching()
    26.     {
    27.         StartCoroutine(requestJoinRandomRoom());
    28.     }
    29.  
    30.     private void updateOtherPlayerStatus()
    31.     {
    32.         if (PhotonNetwork.room.PlayerCount == 2)
    33.         {
    34.             //messageLabel.text = @"Rakip Bulundu";
    35.             //statusLabel.text = @"rakip: " + PhotonNetwork.otherPlayers[0].NickName;
    36.             //cancelButton.interactable = false;
    37.             //SpawnPlayer();
    38.             PhotonNetwork.LoadLevel(1);
    39.  
    40.             if (!PhotonNetwork.isMasterClient)
    41.                 return;
    42.  
    43.             this.photonView.RPC("AddPlayer",PhotonTargets.All);
    44.         }
    45.     }
    46.     /// <summary>
    47.     /// Called when something causes the connection to fail (after it was established).
    48.     /// See the official Photon docs for more details.
    49.     /// </summary>
    50.     public override void OnConnectionFail(DisconnectCause cause)
    51.     {
    52.         if (connectionFailedEvent != null)
    53.             connectionFailedEvent();
    54.     }
    55.  
    56.     public override void OnConnectedToMaster()
    57.     {
    58.         //set my own name and try joining a game
    59.         PhotonNetwork.playerName = "Player"+ UnityEngine.Random.Range(1,9999);
    60.         PhotonNetwork.JoinLobby();
    61.     }
    62.  
    63.     public override void OnPhotonRandomJoinFailed(object[] codeAndMsg)
    64.     {
    65.         Debug.Log("Photon did not find any matches on the Master Client we are connected to. Creating our own room... (ignore the warning above).");
    66.  
    67.         PhotonNetwork.CreateRoom(null, new RoomOptions() { MaxPlayers = 2}, null);
    68.     }
    69.  
    70.     public override void OnPhotonCreateRoomFailed(object[] codeAndMsg)
    71.     {
    72.         if (connectionFailedEvent != null)
    73.             connectionFailedEvent();
    74.     }
    75.  
    76.     public override void OnCreatedRoom()
    77.     {
    78.         Debug.Log("OnCreatedRoom");
    79.  
    80.         Hashtable roomProps = new Hashtable();
    81.         roomProps.Add(RoomExtensions.size, new int[RoomExtensions.initialArrayLength]);
    82.         roomProps.Add(RoomExtensions.score, new int[RoomExtensions.initialArrayLength]);
    83.         PhotonNetwork.room.SetCustomProperties(roomProps);
    84.  
    85.     }
    86.  
    87.     public override void OnJoinedLobby()
    88.     {
    89.         Debug.Log("OnJoinedLobby");
    90.         //statusLabel.text = @"Sen: " + PhotonNetwork.playerName;
    91.         StartMatching();
    92.     }
    93.  
    94.     public override void OnJoinedRoom()
    95.     {
    96.         PhotonPlayer player = GetComponent<PhotonView>().owner;
    97.         Debug.Log("Joined player(id: { player.ID }) in this room.");
    98.         updateOtherPlayerStatus();
    99.     }
    100.  
    101.     //IEnumerator WaitForSceneChange()
    102.     //{
    103.     //    while (SceneManager.GetActiveScene().buildIndex != onlineSceneIndex)
    104.     //    {
    105.     //        yield return null;
    106.     //    }
    107.  
    108.     //    //we connected ourselves
    109.     //    OnPhotonPlayerConnected(PhotonNetwork.player);
    110.     //}
    111.     public override void OnPhotonPlayerConnected(PhotonPlayer player)
    112.     {
    113.         updateOtherPlayerStatus();
    114.     }
    115.  
    116.  
    117.     //received from the master client, for this player, after successfully joining a game
    118.     [PunRPC]
    119.     void AddPlayer()
    120.     {
    121.         RCC_CarControllerV3 testArac;
    122.  
    123.         GameObject[] point = GameObject.FindGameObjectsWithTag("spawnpoint");
    124.         int spawPointindex = UnityEngine.Random.Range(0, point.Length);
    125.  
    126.         testArac = PhotonNetwork.Instantiate(car[CarSelected].name, point[spawPointindex].transform.position, point[spawPointindex].transform.rotation, 0).GetComponent<RCC_CarControllerV3>();
    127.  
    128.         RCC.RegisterPlayerVehicle(testArac);
    129.         RCC.SetControl(testArac, true);
    130.  
    131.         if (RCC_SceneManager.Instance.activePlayerCamera)
    132.             RCC_SceneManager.Instance.activePlayerCamera.SetTarget(testArac.gameObject);
    133.  
    134.         Gunes.instance.playerNameText = PhotonNetwork.playerName;
    135.     }
    136.     public override void OnPhotonPlayerDisconnected(PhotonPlayer player)
    137.     {
    138.         //only let the master client handle this connection
    139.         if (!PhotonNetwork.isMasterClient)
    140.             return;
    141.  
    142.         Gunes.instance.target = null;
    143.     }
    144.  
    145.     public override void OnDisconnectedFromPhoton()
    146.     {
    147.         if (SceneManager.GetActiveScene().buildIndex != offlineSceneIndex)
    148.             SceneManager.LoadScene(offlineSceneIndex);
    149.     }
    150.  
    151. }