- Home /
Photon only shows host their changes to players, not other way around
I'm trying out photon for the first time. I work with vuforia and a marker. A cube spawns on the marker and you can move it with a canvas button.
Whenever the host clicks the button, the other player can also see the changes. But when the player clicks the button it moves for a second and then goes back. At my host their screen you can't see any movement done by the player.
Are there good tutorials to simply sync changes with photon? (it's on android)
Here's a simple script to try it out, which I'm using atm:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
public class Change : MonoBehaviour, IPunObservable
{
public void buttonpress()
{
transform.position = new Vector3(transform.position.x, transform.position.y + .5f, transform.position.z);
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(transform.position);
}
else if (stream.IsReading)
{
transform.position = (Vector3)stream.ReceiveNext();
}
}
}
Answer by Captain_Pineapple · Sep 25, 2019 at 03:45 PM
Hey there,
to work with networking and photon in specific you have to understand a basic principal of synchronization: It always happens in one direction by default!
Think about it like this. If Client A and B were to move the cube at the same time to a new position, whose position would be the "true" position? One has to be discarded to make sure that you do not drop out of sync. But who decides to do this?
For this reason you have the photonview with its stream. The stream has a sending side (where you have stream.IsWriting
and a receiving side with stream.IsReading
. This way you make sure the sendings position is always the true position. This implies though, that you can NOT modifie the position of the cube directly on the receivers side, because every 1/10 second when an update arrives, the position will be reset to the position you sent from the senders side. (which is what you described to be your observations)
So how to fix this:
What you need is some additional update method. Easiest way: Use an RPC which sends a relative position change to the objects owner/sender. For the usage of RPCs photon provides a simple documentation. It does not go too far into detail but should be good enough for your problem. RPC Documentation
Note at this point that each RPC might end up as an additional message so do not generate RPCs each Update.
In general your code has to change into something like this:
public void buttonpress()
{
Vector3 change = new Vector3(0, 0.5f, 0);
//check if we are the owner/sender on this object:
if (photonView.IsMine)
{
transform.position += change;
}
else
{
photonView.RPC("posChangeRPC", photonView.Owner, change);
}
}
[PunRPC]
public void posChangeRPC(Vector3 posChange)
{
transform.position += posChange;
}
Not tested and only spent half a though on this so you might have to tweak it here and there!
Your answer
Follow this Question
Related Questions
Photon only host changes sync 0 Answers
Syncing FPS Firing Sound over network via RPC {PHOTON} 1 Answer
[PUN] Syncing Rigidbodies across network. 1 Answer
RPC with Random Numbers! 2 Answers
Unity Editor Crash !!! 0 Answers