How Update The View of a Player to Other Players to See?

Options
Hello,

I am working on a real time multiplayer project with Unity 3D. It is 2D game. I change the view of the player by changing it's sprite. But when i change the sprite of the local player and when i join the lobby, and when i connect that lobby from different device, i can't see the change the view of the other player. So the question is, how can i update the change the view of the player when it join the lobby? Or how can i update the other player's view change from local device?

Comments

  • vadim
    Options
    Player's appearance is an object property and as any other property could be synchronized across network by different means, via RPC or player properties e.g. Having this property synchronized, apply it on object locally (change sprite accordingly).
  • What kind of RPC call should i use for this update?
  • [CODE]

    public static int playerViewInt;
    public GameObject mainOutline;
    public GameObject top;
    public Sprite[] outlineSprite;
    public Sprite[] topSprite;

    public void startChangeView()
    {
    playerViewInt = PlayerPrefs.GetInt("PlayerViewINT");
    PhotonView photonView = PhotonView.Get(this);
    photonView.RPC("changeView", PhotonTargets.All, null);
    changeView();

    }

    [RPC]
    public void changeView()
    {

    if(playerViewInt == 0)
    {
    mainOutline.transform.GetComponent().sprite = outlineSprite[0];
    top.transform.GetComponent().sprite = topSprite[0];
    }
    if(playerViewInt == 1)
    {
    mainOutline.transform.GetComponent().sprite = outlineSprite[1];
    top.transform.GetComponent().sprite = topSprite[1];

    }
    if(playerViewInt == 2)
    {
    mainOutline.transform.GetComponent().sprite = outlineSprite[2];
    top.transform.GetComponent().sprite = topSprite[2];
    }

    if(playerViewInt == 3)
    {
    mainOutline.transform.GetComponent().sprite = outlineSprite[3];
    top.transform.GetComponent().sprite = topSprite[3];
    }

    }
    [CODE/]

    Here you can see my code. When i use this code and change sprite of a player from local device and after i join the lobby, i can see that the other player wears same sprite like the local device's player. I know there is something wrong with this "photonView.RPC("changeView", PhotonTargets.All, null);" code but i couldn't figure what could be the problem.
  • Hello again. I solved the problem but this time i faced with an another problem. Here is what i use to fix sprite update below. This time the problem that face is, when i start local and second device for the first time by clicking on start multiplayer button, the sprite of player which is in second device, disappears after the players connect to main game screen. I leave the room after that and start an another multiplayer game and after that i start seeming. What could be the problem on the first time connect?


    public void startChangeView()
    {
    playerViewInt = PlayerPrefs.GetInt("PlayerViewINT");

    changeView();
    }


    public void changeView()
    {
    if (photonView.isMine)
    {
    if(playerViewInt == 0)
    {
    photonView.RPC("changeSprite0", PhotonTargets.AllBuffered, null);
    PlayerPrefs.SetInt("PlayerViewINT", playerViewInt);
    }
    if(playerViewInt == 1)
    {
    photonView.RPC("changeSprite1", PhotonTargets.AllBuffered, null);
    PlayerPrefs.SetInt("PlayerViewINT", playerViewInt);
    }
    if(playerViewInt == 2)
    {
    photonView.RPC("changeSprite2", PhotonTargets.AllBuffered, null);
    PlayerPrefs.SetInt("PlayerViewINT", playerViewInt);
    }

    if(playerViewInt == 3)
    {
    photonView.RPC("changeSprite3", PhotonTargets.AllBuffered, null);
    PlayerPrefs.SetInt("PlayerViewINT", playerViewInt);
    }

    }
    else
    {
    return;
    }

    }


    [RPC] public void changeSprite0()
    {
    mainOutline.transform.GetComponent().sprite = outlineSprite[0];
    top.transform.GetComponent().sprite = topSprite[0];
    }
    [RPC] public void changeSprite1()
    {
    mainOutline.transform.GetComponent().sprite = outlineSprite[1];
    top.transform.GetComponent().sprite = topSprite[1];
    }
    [RPC] public void changeSprite2()
    {
    mainOutline.transform.GetComponent().sprite = outlineSprite[2];
    top.transform.GetComponent().sprite = topSprite[2];
    }
    [RPC] public void changeSprite3()
    {
    mainOutline.transform.GetComponent().sprite = outlineSprite[3];
    top.transform.GetComponent().sprite = topSprite[3];
    }
  • vadim
    Options
    Trace what's called and where with logs. Do startChangeView() get called? What is the value of playerViewInt. Same for all RPCs.
    It's hard to say what's wrong just looking at the code.
    This code could be simpler if you used parameters instead of multiple RPCs.
    Also, usage of player properties looks more appropriate and natural for player appearance parameter. They are easier to handle.
  • Hello again,

    Sprites are changing and both player can have different sprites. It is all good with sprites. I solved the second problem; "Hello again. I solved the problem but this time i faced with an another problem. Here is what i use to fix sprite update below. This time the problem that face is, when i start local and second device for the first time by clicking on start multiplayer button, the sprite of player which is in second device, disappears after the players connect to main game screen. I leave the room after that and start an another multiplayer game and after that i start seeming. What could be the problem on the first time connect?" by adding photonView to GameScreen and by adding the player prefab to that photonView. Now all players seems on the screen but i am curious about something which if i did something wrong by doing this? Can you please tell me it is true to do that or if it is wrong?
  • vadim
    Options
    What is GameScreen? I don't see how what you did can solve the problem. But if it works and does not require much resources, better leave it as is.