question

rinktacular avatar image
rinktacular asked

Unity, PlayFab, and Photon - Players do not see each other when joining rooms

I am working on a multiplayer game in Unity which is using Playfab and the Authentication and Photon which is hosting the multiplayer. I can successfully get players into the same room and I can load the scene after players 'join' the room, however, when 2 players are in the same room, they can not see each other. This is my authentication service:


    public class LoginWithCustomID : MonoBehaviour
    {
        private string _playFabPlayerIdCache;
        private bool _isNewAccount;
        private string _playerName;
        // Use this to auth normally for PlayFab
        void Awake()
        {
            PhotonNetwork.autoJoinLobby = false;
            PhotonNetwork.automaticallySyncScene = true;
            DontDestroyOnLoad(gameObject);
            authenticateWithPlayfab();
        }
        private void authenticateWithPlayfab()
        {
            var request = new LoginWithCustomIDRequest
            {
                CustomId = "CustomId123",
                CreateAccount = true,
                InfoRequestParameters = new GetPlayerCombinedInfoRequestParams()
                {
                    GetUserAccountInfo = true,
                    ProfileConstraints = new PlayerProfileViewConstraints()
                    { ShowDisplayName = true }
                }
            };
            PlayFabClientAPI.LoginWithCustomID(request, requestPhotonToken, OnLoginFailure);
        }
        private void requestPhotonToken(LoginResult result)
        {
            PlayerAccountService.loginResult = result;
            _playFabPlayerIdCache = result.PlayFabId;
            _playerName = result.InfoResultPayload.AccountInfo.TitleInfo.DisplayName;
            if (result.NewlyCreated)
            {
                _isNewAccount = true;
                setupNewPlayer(result);
            }
            PlayFabClientAPI.GetPhotonAuthenticationToken(new GetPhotonAuthenticationTokenRequest()
            {
                PhotonApplicationId = "d090b4a8-35dc-41de-b33b-748861e04ccb"
            }, AuthenticateWithPhoton, OnLoginFailure);
        }
        private void setupNewPlayer(LoginResult result)
        {
            PlayFabClientAPI.UpdateUserData(
                new UpdateUserDataRequest()
                {
                    Data = new Dictionary<string, string>()
                    {
                        { "Level", "1" },
                        { "xp", "0" }
                    }
                }, success =>
                {
                    Debug.Log("Set User Data");
                }, failure =>
                 {
                     Debug.Log("Failed to set User Data..");
                 }
            );
        }
        private void AuthenticateWithPhoton(GetPhotonAuthenticationTokenResult result)
        {
            Debug.Log("Photon token acquired: " + result.PhotonCustomAuthenticationToken);
            var customAuth = new AuthenticationValues { AuthType = CustomAuthenticationType.Custom };
            customAuth.AddAuthParameter("username", _playFabPlayerIdCache);
            customAuth.AddAuthParameter("token", result.PhotonCustomAuthenticationToken);
            PhotonNetwork.AuthValues = customAuth;
            setNextScene();           
        }
        private void setNextScene()
        {
            if(_isNewAccount || _playerName == null)
            {
                SceneManager.LoadSceneAsync("CreatePlayerName", LoadSceneMode.Single);
            }
            else
            {
                SceneManager.LoadSceneAsync("LandingScene", LoadSceneMode.Single);
            }
        }
        private void OnLoginFailure(PlayFabError error)
        {
            Debug.LogWarning("something went wrong in auth login");
            Debug.LogError("Here's some debug info:");
            Debug.LogError(error.GenerateErrorReport());
        }
    }
}


This all works and a player is logged into PlayFab, as well as Photon I would assume if I got the Photon auth token. This brings me to my landing scene, which is essentially a place for an authenticated user to click a button to join a random room via Photon:

    public static GameManager instance;
    public static GameObject localPlayer;
    private void Awake()
    {
        if (instance != null)
        {
            DestroyImmediate(instance);
            return;
        }
        DontDestroyOnLoad(gameObject);
        instance = this;
        PhotonNetwork.automaticallySyncScene = true;
    }
    // Use this for initialization
    void Start()
    {
        PhotonNetwork.ConnectUsingSettings("A_0.0.1");
    }
    public void JoinGame()
    {
        RoomOptions ro = new RoomOptions();
        ro.MaxPlayers = 4;
        PhotonNetwork.JoinOrCreateRoom("Test Room 2", ro, null);
    }
    public override void OnJoinedRoom()
    {
        Debug.Log("Joined Room!");
        if (PhotonNetwork.isMasterClient)
        {
            PhotonNetwork.LoadLevel("Test_Map1");
        }
    }
    private void OnLevelWasLoaded(int level)
    {
        if (!PhotonNetwork.inRoom)
            return;
        localPlayer = PhotonNetwork.Instantiate(
            "Player",
            new Vector3(0, 1f, 0),
            Quaternion.identity,
            0);
    }
    public void LeaveRoom()
    {
        PhotonNetwork.LeaveRoom();
        SceneManager.LoadScene("LandingScene", LoadSceneMode.Single);
    }




This loads the scene that I named "Test_scene1" successfully and I show within my scene, the room name and number of active players in the room.  When I do a run and build, I get a user's playerPrefab to load into the room.  When I run the game through unity, I can get a second player to log into the room.  The problem is, the players do not see eachother and I can not figure out why that is.  I am following the PLayerfab/Photon tutorials on their respective sites, but I can't find anything that I did wrong in either one.
From what I read, it looks like my instantiate method might be wrong but I'm not sure why. Below is my player Prefab showing the components attached to it:


I apologize for this huge question, I just wanted to provide as much information as I could.
unity3dphotonMatchmaking
capture.png (72.8 KiB)
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

rinktacular avatar image rinktacular commented ·

For a little but of clarity, even in Unity when I load "Test_Map1" I even see that there are two players in the inspector when I have two instances running and that I can only control one of them. But I do not actually see the other player.

0 Likes 0 ·
rinktacular avatar image rinktacular commented ·

Turns out, the Prefabs are loading in 'deactived' I placed a ball in the middle of my test map and each player could see the ball moving but could not see the player who was pushing it, causing it to move.. So the connection is working fine, but for some reason my player prefabs are loading in 'deactivated' which is why I can't see them.

0 Likes 0 ·

1 Answer

·
pfnathan avatar image
pfnathan answered

Thanks for the findings, You might need to ping folks at Unity about prefab being loaded as deactivated state though.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

rinktacular avatar image rinktacular commented ·

Well, at least someone else recognizes my pain! haha Will do, thanks

1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.