How do you control a script with a remote player?

How do I get the position of a GameObject that I created to be controlled by another player? I can handle the selection of it and movement on the other connection, but I'd like to know how to communicate that across the network.

All the examples I've seen have something like:

[code2=csharp]void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo
if (stream.isWriting)
//We own this player: send the others our data
//Network player, receive data
controllerScript._characterState = (CharacterState)(int)stream.
correctPlayerPos = (Vector3)stream.ReceiveNext();
correctPlayerRot = (Quaternion)stream.ReceiveNext();
This code is the opposite of what I want sometimes. How do I send the data for a network player and receive it for a player that this instance owns?


  • That's not built in, so you can't without changing PUN and that's a bit more complex.
    You would have to override the owner of an object. In best case, you would pass ownership from current owner to the new one, so it's always defined who owns the object. Verying lag per player means not everyone has the same view of things: Someone will say "it's now owned by newowner" while at the same time another player still thinks "oldowner is the owner".

    Alternatively you can use PUN (or the layer below that: the Photon Unity SDK) to just send your messages. You can do all the ownership handling you want and skip whatever PUN defines as rules.
  • Tobias wrote:
    That's not built in, so you can't without changing PUN and that's a bit more complex.
    Thanks for letting me know that there is nothing built-in that will handle it. I ended up using an RPC to the PhotonView's owner, so that the owner can update it to what it should be. It seems to work. I hope it doesn't have any bad side-effects.
  • Aside from some additional messages, this doesn't have really bad side effects.
    It's always good to test this stuff in a worst case scenario / test. With as many users and as hectic a game as it can become.
    In best case, you use the RPCs to hand over control of the object. If any user might control some object for longer than just one "kick" or "move" or something.
Sign In or Register to comment.