- Home /
Question by
calender68 · Apr 04, 2019 at 12:20 AM ·
networkingpositionphotonsynchronizationmoving platform
Photon Network syncing the transform of a moving platform
I am using Photon Unity Networking for my game and in one of the levels, there is a moving elevator platform that goes up and down. When played with other players, player 1 (the master client) has the platform moving ahead of the other player's games, while player 2, 3, and 4 has the platform's position perfectly synced. How do I sync the position of the platform for all players? Here is my elevator script:
public class Elevator : MonoBehaviour
{
public Transform elevator;
public Transform position1;
public Transform position2;
public Vector3 newPosition;
public string currentState;
public float smooth;
public float resetTime;
private Vector3 TargetPosition;
// Use this for initialization
void Start()
{
ChangeTarget();
//makes the platform move on start
}
// Update is called once per frame
void FixedUpdate()
{
elevator.position = Vector3.Lerp(elevator.position, newPosition, smooth * Time.deltaTime);
//moves the platform
}
private void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
//this is where i tried to fix it with OnPhotonSerializeView but it doesn't work
if (stream.isWriting)
{
stream.SendNext(transform.position);
}
else
{
elevator.position = (Vector3)stream.ReceiveNext();
}
}
private void SyncMove()
{
elevator.transform.position = Vector3.Lerp(elevator.transform.position, TargetPosition, 0.5f);
}
void ChangeTarget()
{
if (currentState == "Moving to first floor")
{
currentState = "Moving to second floor";
newPosition = position2.position;
}
else if (currentState == "Moving to second floor")
{
currentState = "Moving to first floor";
newPosition = position1.position;
}
else if (currentState == "")
{
currentState = "Moving to second floor";
newPosition = position2.position;
}
Invoke("ChangeTarget", resetTime);
}
}
Comment