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 Quite MAGICAL player instantiation with Photon, need help!

Discussion in 'Multiplayer' started by DaveA, Apr 1, 2021.

  1. DaveA

    DaveA

    Joined:
    Apr 15, 2009
    Posts:
    310
    I've got a little game I'm building, using the Photon Asteroid example as a model. that is, it has a 'lobby' where players gather, then enter the main scene.
    When I load the main scene I instantiate player avatars. I must be doing something wrong, but it's spooky what happens:

    Code (CSharp):
    1.  
    2.  
    3.             Player[] sorted = PhotonNetwork.PlayerList.OrderBy((p) => p.ActorNumber).ToArray();
    4.             Debug.Log(sorted.Length + " users in room");
    5.             foreach (Player p in sorted)
    6.             {
    7.                 Debug.Log(string.Format("{0} {1}: is local {2}, is masterclient {3}, ", p.NickName, p.UserId, p.IsLocal, p.IsMasterClient));
    8.  
    9.                 if (p.IsLocal)
    10.                 {
    11.                     GameObject go = PhotonNetwork.Instantiate(playerPrefab.name, Camera.main.transform.position, Quaternion.identity, 0);
    12.                     go.transform.parent = Camera.main.transform;
    13.                     go.name = p.NickName;
    14.                     go.SendMessage("SetAvatarName", p.NickName); ;
    15.                 }
    16.             }
    17.  
    I'm assuming I'm only going to instantiate my avatar when it's 'local' (that's 'me' right?). In fact, I maybe don't even need to see 'me' so I could avoid this?

    Well here's the spooky part. If I join one player from remote, I have 2 players when I enter this scene. One isLocal, the other is not. So, only ONE (the local one) should be created, yes? BUT TWO are created. And I cannot figure out where it's coming from. This is the only place in the game I reference playerPrefab, and it's in my class (unless it's hiding a same-named object in a base class???) Anyway, the code that sets the name and avatar label does NOT get executed on the remote user. Everything else about the remote player works fine, I can see him moving properly about the scene.

    Any insights welcome.

    Edit: Or, is it that the PhotonNetwork.Instantiate creates this from the remote? But it doesn't have the logic/data in place to set the name. And maybe that's handled by PlayerCustomProperties??
     
    Last edited: Apr 1, 2021
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Yes and likely yes.
    The network instantiate will tell everyone what you instantiated.
    Locally, you can disable the looks, place the camera differently and more but you need the "original" of an object to send updates about it.
    At least in PUN, you need this when you want to use PhotonViews.

    You can also skip using PhotonViews and just send updates you define. As in "I am here, looking there", etc.
    Then, you would possibly use RaiseEvent.

    The Basics Tutorial explains what PhotonNetwork.Instantiate does and how you use input only for the local character, etc.
    This page has a short intro to using RaiseEvent.