Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party PhotonNetwork - Second Player can't play

Discussion in 'Multiplayer' started by vegoattack, May 10, 2019.

  1. vegoattack

    vegoattack

    Joined:
    May 9, 2019
    Posts:
    1
    Hey everyone, I'm sorry for my English.
    this is my first game, I create something similar to counter-strike
    I have a problem with the other player,
    When I start the game in the unity I connect to the server, the level is loaded, I can walk, etc. But when I run the "second game" built. I click play, everything loads, connects, the shoe does not read the level and does not spawn the player. Here is the screen and the code, sorry that I wrote Polish in the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. /// <summary>
    7. /// Skrypt odpowiadający za połączenie się z serwerem (oraz rozłączanie się i utratę połączenia)
    8. /// </summary>
    9. public class MenadzerPolaczen : Photon.MonoBehaviour
    10. {
    11.     GameManager gm;
    12.  
    13.     void Start()
    14.     {
    15.         gm = GetComponent<GameManager>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         if (Input.GetKeyDown(KeyCode.L))
    21.             Gracz.DebugujListeGraczy();
    22.     }
    23.     public void Polacz()
    24.     {
    25.         PhotonNetwork.sendRate = 30;
    26.         PhotonNetwork.sendRateOnSerialize = 30;
    27.         PhotonNetwork.ConnectUsingSettings("CS_0.1");
    28.     }
    29.     private void OnGUI()
    30.     {
    31.         GUI.Label(new Rect(0, 0, 200, 20), PhotonNetwork.connectionStateDetailed.ToString());
    32.     }
    33.  
    34.     void OnJoinedLobby()
    35.     {
    36.         SceneManager.LoadScene(1);
    37.              
    38.     }
    39.     void OnPhotonRandomJoinFailed()
    40.     {
    41.         PhotonNetwork.CreateRoom(null);
    42.     }
    43.     private void OnLevelWasLoaded(int level)
    44.     {
    45.         if(level != 0)
    46.             PhotonNetwork.JoinRandomRoom();
    47.     }
    48.  
    49.    
    50.     private void OnPlayerConnected(PhotonPlayer pp)
    51.     {
    52.         if (PhotonNetwork.isMasterClient)
    53.         {
    54.             int team = 0;
    55.             if (Gracz.gracze[Gracz.gracze.Count - 1].team == Team.CT)
    56.                 team = 1;
    57.             else
    58.                 team = 0;
    59.                 photonView.RPC("GraczWszedl", PhotonTargets.All, pp, team);
    60.         }
    61.     }
    62.     private void OnPlayerDisconnected(PhotonPlayer pp)
    63.     {
    64.         if (PhotonNetwork.isMasterClient)
    65.         {
    66.             photonView.RPC("GraczWyszedl", PhotonTargets.All, pp);
    67.         }
    68.     }
    69.  
    70.      [PunRPC]
    71.     void GraczWszedl(PhotonPlayer pp, int team)
    72.     {
    73.         Gracz gracz = new Gracz();
    74.         Gracz.gracze.Add(gracz);
    75.         gracz.nick = pp.NickName;
    76.         gracz.pp = pp;
    77.         gracz.team = (Team) team;
    78.         if (pp == PhotonNetwork.player)
    79.         {
    80.             // To my! W takim razie zespawnujmy naszego gracza!
    81.             Gracz.mojGracz = gracz;
    82.             gm.SpawnujGracza();
    83.         }
    84.        
    85.     }
    86.  
    87.      [PunRPC]
    88.     void GraczWyszedl(PhotonPlayer pp)
    89.     {
    90.         var tmpGracz = Gracz.ZnajdzGracza(pp);
    91.         if(tmpGracz != null)
    92.         {
    93.             Gracz.gracze.Remove(tmpGracz);
    94.         }
    95.     }
    96.    
    97.     void OnCreatedRoom()
    98.     {
    99.         int team = Random.Range(0, 2);
    100.         photonView.RPC("GraczWszedl", PhotonTargets.All, PhotonNetwork.player, team);
    101.     }
    102. }
    103.  








    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityStandardAssets.Characters.FirstPerson;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.        
    16.     }
    17.     public void SpawnujGracza()
    18.     {
    19.         Vector3 pozycjaSpawnu = Vector3.zero;
    20.         Transform spawny = GameObject.Find("Mapa" + UnityEngine.SceneManagement.SceneManager.GetActiveScene().name + "/Spawny/" + Gracz.mojGracz.team.ToString()).transform;
    21.         pozycjaSpawnu = spawny.GetChild(Random.Range(0, spawny.childCount)).position;
    22.         GameObject mojGraczGO = PhotonNetwork.Instantiate("Gracz", pozycjaSpawnu, Quaternion.identity, 0);
    23.         mojGraczGO.GetComponent<FirstPersonController>().enabled = true;
    24.         mojGraczGO.transform.Find("TrzymaczKamery").gameObject.SetActive(true);
    25.         GetComponent<PhotonView>().RPC("SpawnujGraczaRPC", PhotonTargets.All, Gracz.mojGracz.nick, mojGraczGO.GetComponent<PhotonView>().viewID);
    26.     }
    27.  
    28.     [PunRPC]
    29.     void SpawnujGraczaRPC(string nick, int pvID, PhotonMessageInfo pmi)
    30.     {
    31.         GameObject nowyGraczGO = PhotonView.Find(pvID).gameObject;
    32.         nowyGraczGO.name = "Gracz_" + nick;
    33.         Gracz gracz = Gracz.ZnajdzGracza(pmi.sender);
    34.         gracz.gameObject = nowyGraczGO;
    35.     }
    36. }
    37.  


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Gracz
    6. {
    7.     public static Gracz mojGracz;
    8.     public GameObject gameObject;
    9.     public string nick = "";
    10.     public PhotonPlayer pp;
    11.     public Team team;
    12.  
    13.  
    14.     public static List<Gracz> gracze = new List<Gracz>();
    15.  
    16.     public static void DebugujListeGraczy()
    17.     {
    18.         string dbgS = "Debug listy graczy! Ilość graczy: " + gracze.Count + " Wszyscy gracze: ";
    19.         int i = 0;
    20.         foreach (var gracz in gracze)
    21.         {
    22.             dbgS += "ID: " + i + ", nick: " + gracz.nick + ", team: " + gracz.team ;
    23.             i++;
    24.         }
    25.         Debug.Log(dbgS);
    26.     }
    27.     public static Gracz ZnajdzGracza(PhotonPlayer pp)
    28.     {
    29.         for (int i = 0; i < gracze.Count; i++)
    30.         {
    31.             if (gracze[i].pp == pp)
    32.                 return gracze[i];
    33.         }
    34.         return null;
    35.     }
    36. }
    37.  
    38. public enum Team {CT, TT}
     

    Attached Files: