How do you control a script with a remote player?
Options
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
info)
{
if (stream.isWriting)
{
//We own this player: send the others our data
stream.SendNext((int)controllerScript._characterState);
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
//Network player, receive data
controllerScript._characterState = (CharacterState)(int)stream.
ReceiveNext();
correctPlayerPos = (Vector3)stream.ReceiveNext();
correctPlayerRot = (Quaternion)stream.ReceiveNext();
}
}[/code2]
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?
All the examples I've seen have something like:
[code2=csharp]void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo
info)
{
if (stream.isWriting)
{
//We own this player: send the others our data
stream.SendNext((int)controllerScript._characterState);
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
//Network player, receive data
controllerScript._characterState = (CharacterState)(int)stream.
ReceiveNext();
correctPlayerPos = (Vector3)stream.ReceiveNext();
correctPlayerRot = (Quaternion)stream.ReceiveNext();
}
}[/code2]
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?
0
Comments
-
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.0 -
Tobias wrote:That's not built in, so you can't without changing PUN and that's a bit more complex.0
-
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.0