Synchronize scene objects?

Options
I just started using PUN for my game project and can't seem to figure out how to synchronize the position and rotation of dynamic scene objects.
I have these physics based cubes in my scene for testing purposes and their network code works fine for the room host, but not for the connected user.

Here is the network code I am using for the cubes:
[code2=csharp]using UnityEngine;
using System.Collections;

public class CubeNetwork : Photon.MonoBehaviour {

private Vector3 correctPos = Vector3.zero;
private Quaternion correctRot = Quaternion.identity;

void Awake() {
correctPos = transform.position;
correctRot = transform.rotation;
gameObject.name = gameObject.name + photonView.viewID;
}

void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
correctPos = (Vector3)stream.ReceiveNext();
correctRot = (Quaternion)stream.ReceiveNext();
}
}

void Update()
{
if (!photonView.isMine)
{
transform.position = Vector3.Lerp(transform.position, correctPos, Time.deltaTime * 5);
transform.rotation = Quaternion.Lerp(transform.rotation, correctRot, Time.deltaTime * 5);
print ("your turn"+gameObject.name+photonView.isMine);
}
}
}[/code2]

When the room host affects the cube (e.g. shooting at it) it synchronizes just fine, but when the connected user affects the cube it moves back to it's original position and the room host sees no change.

EDIT: Also it seems that if I change
[code2=csharp]if (!photonView.isMine)[/code2]
so that it reads
[code2=csharp]if (photonView.isMine)[/code2]
then it does the opposite behavior where it synchronizes fine with connected user but not with host.

Comments

  • Tobias
    Options
    That's most likely due to ownership. Only the Master can affect scene objects. It's more or less an arbitrary rule to avoid issues with simultaneous , concurring changes to the object's position.
    You could adjust your game to make the Master affect scene objects or modify PUN to use some other rule of ownership.
  • You could adjust your game to make the Master affect scene objects

    By this do you mean I should make it so only the master can affect the cubes and never the remote user? because that would just be kind of pointless in an online game if you ask me.

    How would I go about changing the ownership to be shared across the network?
    Could I come to a solution using RPC's rather than OnPhotonSerializeView?
  • Tobias
    Options
    If that's pointless, depends on the game really. In your case it most likely is.
    Ownership is fully controlled by the clients and what we do in PUN. Photon itself doesn't mind at all but the point is that if you don't have clear ownership, clients can send conflicting position updates.

    You have more or less three options:
    a) Add a control property which overrides the simple ownership of PUN. Per PhotonView, you can set properties. Use a new one to define ownership and then modify the PhotonView.isMine to reflect this new property. Who changes it and when a change is allowed can be defined as needed by you.

    b) Like you said: Don't use a PhotonView. You could control a game object's positions with RPCs alone. You need to keep in mind that lag and simultaneous actions might make RPCs clash.

    c) Switch to using the plain Photon LoadBalancing API we provide in our Unity SDK. This doesn't re-implement Unity's Networking in the first place. You will need to come up with IDs for your objects and events to sync them but it gives you full control. This would be a rather drastic change.
  • PixegaStudio
    Options
    Hi Tobias,

    I know old entry but really I need answer :) Your choice a! add a control property... What does it mean?