Door only opens for other players if Host does it

Options
Hi!

If the host is opening the door, the other players can see it too. But if another player is opening the door, only that player sees it opening no one else?!

This is my Door Code:
using UnityEngine;
using UnityEngine.EventSystems;

using Photon.Pun;

using System.Collections;

public class DoorScript : MonoBehaviourPun, IPunObservable
{
    private bool open =false;
    public float doorOpenAngle = 90f;
    public float doorCloseAngle = 0f;
    public float smooth = 2f;    
 
public AudioClip DoorOpenSound; 
public AudioClip DoorCloseSound; 
public AudioSource AudSource;
  private PhotonView PV;


     #region IPunObservable implementation


            public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
            {
                if (stream.IsWriting)
                {                    
                   stream.SendNext(open);                     
                }
                else
                {                            
                 this.open = (bool)stream.ReceiveNext();                   
                }
            }

    #endregion

    // Start is called before the first frame update
    void Start()
    {
         PV = GetComponent<PhotonView>();
        AudSource = GetComponent < AudioSource > ();
    }
    
    public void ChangeDoorState()
    {
        //Stop the audiosource if it's already playing    
        if (AudSource.isPlaying)
            AudSource.Stop();
        
        if (open)            
           AudSource.clip = DoorCloseSound;  
        else     
            AudSource.clip = DoorOpenSound;      

        open = !open;
        AudSource.Play();
    }

    // Update is called once per frame
    void Update()
    {        
        if(open) 
        {
            Quaternion targetRotation = Quaternion.Euler(0,doorOpenAngle,0);
            this.transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
        }
        else
        {
            Quaternion targetRotation2 = Quaternion.Euler(0,doorCloseAngle,0);
            this.transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation2, smooth * Time.deltaTime);
        }
    }
}


ChangeDoorState get's called in another script:
if(Input.GetKeyDown(KeyCode.Mouse0))
        {
           Ray ray = new Ray(transform.position, transform.forward);
           RaycastHit hit;
           if(Physics.Raycast(ray, out hit, interactDistance)) 
           {
               if(hit.collider.CompareTag("Door"))               
                   hit.collider.transform.GetComponent<DoorScript>().ChangeDoorState();                          
           }
        }

I also tried using RPC but it was the same problem.

Comments

  • Yuber
    Options
    I fixed it now. But the door when opened by clients is not closing properly, there is a small space open. Why? Also if the clients open the door it is lagging kinda.

    I used this:
    if(PhotonNetwork.IsMasterClient) 
            open = !open;
        else
         PV.RPC("changeDoorClient",RpcTarget.AllViaServer, !open);
    
      [PunRPC]
         void changeDoorClient(bool puInGameSync)
         {
             // update this variable on every client
             open = puInGameSync;
         }