Please help, syncing door rotation problem

i've been posting so many quetions but with no reply, maybe because the scripts that i've been using is kinda complicated, anw here's another working script but the only problem with this one is rotation syncing, the door is kinda laggy when its open, only when two or more player join but if im playing solo it will open smoothly with no problem at all





using UnityEngine;
using System.Collections;
using Photon.Pun;
[RequireComponent(typeof(PhotonView))]


public class DoorTest : MonoBehaviourPun
{

public PhotonView pv;
public float smooth = (float)5.0;
public float DoorOpenAngle = (float)90.0;
public float DoorCloseAngle = (float)0.0;
public bool open = false;
public bool enter = false;
public string defined_key = "f";
public AudioClip OpenDoorSound;
public AudioSource ASource;

void Start()
{
//Create PhotonView
this.gameObject.AddComponent<PhotonView>();
//PhotonView photonView = PhotonView.Get(this);



}

void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
(enter) = true;
}
}

void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
(enter) = false;
}
}

void Update()
{

if (open == true)
{
photonView.RPC("openDoor", RpcTarget.All, null);
}
else
{
photonView.RPC("closeDoor", RpcTarget.All, null);
}

if (enter == true && Input.GetKeyDown(defined_key))
{
open = !open;
ASource.PlayOneShot(OpenDoorSound, 0.7F);
}

}

[PunRPC]
void closeDoor()
{
var target1 = Quaternion.Euler(transform.rotation.x, DoorCloseAngle, transform.rotation.z);
transform.rotation = Quaternion.Slerp(transform.rotation, target1, Time.deltaTime * smooth);
}

[PunRPC]
void openDoor()
{
var target = Quaternion.Euler(transform.rotation.x, DoorOpenAngle, transform.rotation.z);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}

void OnGUI()
{
if (enter)
{
GUI.Label(new Rect(Screen.width / 2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door");
}
}
}

Answers

  • how bout

    DoorTest : MonobehaviorPun, IPunObservable

    {

    float NetRecYRot;
    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
    if (stream.IsWriting)
    {
    stream.sendNext(transform.localRotation.y);
    }
    else
    {
    NetRecYRot = (float)stream.ReceiveNext();
    }

    public void Update()
    {
    do door stuff here with sounds and things based on the NetRecYRot parameter, which will always be synchronized!
    }