Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug Photon Engine Multiplayer Issues

Discussion in 'Lobby' started by sonarshreeprasad, Apr 14, 2023.

  1. sonarshreeprasad

    sonarshreeprasad

    Joined:
    Feb 12, 2023
    Posts:
    2
    Hello,

    I am new at unity and using this code for my room manager

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    using Photon.Pun;
    using Photon.Realtime;

    public class RoomManager : MonoBehaviourPunCallbacks
    {
    public static RoomManager instance;

    public GameObject AudioManager;

    public GameObject player;
    [Space]
    public Transform[] spawnPoints;

    [Space]
    public GameObject roomCam;

    public GameObject connectingUI;

    public GameObject gameStartMenuCanvas;
    public GameObject inventoryMenuCanvas;
    public GameObject gameSettingsMenuCanvas;
    public GameObject inGameMenuCanvas;
    public GameObject playerSpeedManager;
    public GameObject gameMenuEventSystem;

    public GameObject startGameButton;
    public GameObject playerSpeedSettingsButton;
    public GameObject taskManager;

    private string nickname = "Astroboy";

    void Awake() {
    instance = this;
    }

    void Start()
    {
    inGameMenuCanvas.SetActive(false);
    gameSettingsMenuCanvas.SetActive(false);
    inventoryMenuCanvas.SetActive(false);
    gameStartMenuCanvas.SetActive(true);
    gameMenuEventSystem.SetActive(true);

    // StartGameButtonHandler();
    }

    public void StartGameButtonHandler()
    {
    Debug.Log("Start Game Button Pressed");
    gameStartMenuCanvas.SetActive(false);
    gameMenuEventSystem.SetActive(false);
    JoinRoomButtonPressed();
    }

    public void GameSettingsButtonHandler()
    {
    Debug.Log("Game Settings Button Pressed");
    gameStartMenuCanvas.SetActive(false);
    gameSettingsMenuCanvas.SetActive(true);

    gameMenuEventSystem.GetComponent<EventSystem>().SetSelectedGameObject(playerSpeedSettingsButton);
    }

    public void QuitGame()
    {
    Debug.Log("QuitGame() called");
    Application.Quit();
    }

    public void ChangeNickname(string _name){
    nickname = _name;
    }

    public void JoinRoomButtonPressed(){
    Debug.Log(message:"Connecting...");

    // Theres a master server and once we are connected to this we can join diff rooms
    PhotonNetwork.ConnectUsingSettings();

    connectingUI.SetActive(true);
    }

    public override void OnConnectedToMaster()
    {
    base.OnConnectedToMaster();

    Debug.Log(message:"Connected to Server");

    PhotonNetwork.JoinLobby();
    }

    public override void OnJoinedLobby()
    {
    base.OnJoinedLobby();

    Debug.Log(message:"Joined Lobby");

    PhotonNetwork.JoinOrCreateRoom(roomName: "test", roomOptions: null, typedLobby: null);
    }

    public override void OnJoinRoomFailed(short returnCode, string message)
    {
    base.OnJoinRoomFailed(returnCode, message);

    Debug.LogError("Failed to join room: " + message);
    }

    public override void OnJoinedRoom()
    {
    base.OnJoinedRoom();

    Debug.Log(message:"Joined Room");

    roomCam.SetActive(false);

    SpawnPlayer();
    }

    public void SpawnPlayer(){
    Transform spawnPoint = spawnPoints[UnityEngine.Random.Range(0, spawnPoints.Length)];
    GameObject _player = PhotonNetwork.Instantiate(player.name, spawnPoint.position, Quaternion.Euler(0,180f,0)); // In Quaternion plater is making 180* turn, else use Quaternion.Identity
    _player.GetComponent<PlayerSetup>().IsLocalPlayer(); // will only be called on local player

    // print("Numvber of players : " + PhotonNetwork.CurrentRoom.PlayerCount);


    _player.GetComponent<PhotonView>().RPC("SetNickname", RpcTarget.AllBuffered, nickname + " " + PhotonNetwork.CurrentRoom.PlayerCount);
    AudioManager.GetComponent<AudioManager>().PlaySound(0);

    playerSpeedManager.GetComponent<PlayerSpeedManager>().SetInitialLoadPlayerSpeed();

    // taskManager.GetComponent<TaskCompletionMsg>().TriggerIntroMsgCanvas();

    StartCoroutine(IntoMsgCoroutine());
    }

    IEnumerator IntoMsgCoroutine()
    {
    yield return new WaitForSeconds(2f); // 2 seconds

    taskManager.GetComponent<TaskCompletionMsg>().TriggerIntroMsgCanvas();
    }
    }

    and this code for player setup


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Photon.Pun;
    using TMPro;

    public class PlayerSetup : MonoBehaviour
    {
    public CharacterMovement movement;
    public GameObject camera;
    public string nickname;
    public TextMeshPro nicknameText;

    public void IsLocalPlayer(){
    movement.enabled = true;
    camera.SetActive(true);
    }

    [PunRPC]
    public void SetNickname(string _name){
    nickname = _name;
    nicknameText.text = nickname;
    }
    }

    When I am connecting using two devices both the players are joining different rooms and OnJoinRoomFailed is not helping as well and I have just one photon app so they are using same photon id.
     
  2. sonarshreeprasad

    sonarshreeprasad

    Joined:
    Feb 12, 2023
    Posts:
    2
    Update:
    I solved this issue but while I play, changes in the game done by player 1 are not reflected for player 2, and as long as there is one player everything works fine but when the second player is added their controls are swapped and the camera shows the other player screen.
     
  3. veleek_unity

    veleek_unity

    Ben Randall Unity Technologies

    Joined:
    Aug 25, 2021
    Posts:
    59
    This forum is specifically for the UGS Lobby Service. I believe that you're asking a question that's related to Photon which isn't owned Unity, so you might be better off reaching out on their forums: https://forum.photonengine.com/sso
     
    Mj-Kkaya likes this.