Locating the avatars in a Unity scene?

First understand everything is working I just need to clarify a few basic things that I'm still unclear about. Imagine a scene with 3 players. Clearly PUN knows about them and has 3 Player objects with properties. Unity knows about the player avatars or we wouldn't be seeing them.

So is there a direct relationship between the PUN Player object and the avatar used to represent that player that permits one to locate the player avatar using a Player.ActorNumber of something like it?

Are the avatars simply Game Objects like any other?

As an exercise I'm trying to locate the avatar of the player that made the RPC method call. I am passing the ActorNumber at the moment.

Best Answers

  • Tobias
    Tobias admin
    Answer ✓

    is there a direct relationship between the PUN Player object and the avatar used to represent that player

    No, there is none. You could set up something like it. When you call PhotonNetwork.Instantiate, the local client's new object will be returned (just like Unity does in general). So this is one that you control and you could store a reference to it.

    Also, there is a "magic" callback OnPhotonInstantiated. If you implement this in a script and put it on the prefab, this is called whenever such a prefab gets instantiated. This way, you can make avatars register themselves (in some dict or list you create) for each player there is (the instantiated callback is also called when another client instantiated a prefab and this client just creates a copy/proxy).

    Are the avatars simply Game Objects like any other?

    Yes. They just have a PhotonView component and maybe some more, related to networking.

  • Tobias
    Tobias admin
    Answer ✓

    You could also just store the avatars per player via their PhotonView.viewID.

Answers

  • Tobias
    Tobias admin
    Answer ✓

    is there a direct relationship between the PUN Player object and the avatar used to represent that player

    No, there is none. You could set up something like it. When you call PhotonNetwork.Instantiate, the local client's new object will be returned (just like Unity does in general). So this is one that you control and you could store a reference to it.

    Also, there is a "magic" callback OnPhotonInstantiated. If you implement this in a script and put it on the prefab, this is called whenever such a prefab gets instantiated. This way, you can make avatars register themselves (in some dict or list you create) for each player there is (the instantiated callback is also called when another client instantiated a prefab and this client just creates a copy/proxy).

    Are the avatars simply Game Objects like any other?

    Yes. They just have a PhotonView component and maybe some more, related to networking.

  • Timing couldn't be better. I finally realized I can inspect the scene while it is running (I can be slow) and I see my avatar object and can read (and even set) the properties. I was thinking that I might just tag the avatars with a "Player" tag which should permit fetching them all but I can see how adding them and removing them from a collection would be more efficient.

    I also figure that I could add a property to the avatar to hold onto the ActorNumber but I think I like the idea of using it as a key in a dictionary.

    I'm having quite the time applying my C# knowledge to the world of VR :-)

  • Tobias
    Tobias admin
    Answer ✓

    You could also just store the avatars per player via their PhotonView.viewID.

  • Thanks that should work as well perhaps better.