Search Unity

Sync Door Animation Pun

Discussion in 'Multiplayer' started by Batuhan13, May 19, 2021.

  1. Batuhan13

    Batuhan13

    Joined:
    Apr 9, 2017
    Posts:
    116
    Hi there mate I am new to multiplayer coding and I am trying to animate a door in a scene when player press f with raycast. I coded door script basically and it works perfectly in solo mode here is the my door code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class SimpleDoor : MonoBehaviour
    7. {
    8.  
    9.     [SerializeField] private Animator _doorAnimator = null;
    10.     [Header("0 for Inside 1 for Outside")]
    11.     [Range(0,1)]
    12.     [SerializeField] private int _openingFace = 0;
    13.     [SerializeField] private bool _isOpenable = false;
    14.     [SerializeField] private Collider _doorCollider = null;
    15.  
    16.     public void OpenDoor()
    17.     {
    18.         if (_isOpenable == true)
    19.         {
    20.             this.gameObject.tag = "Untagged";
    21.             _doorCollider.isTrigger = true;
    22.             if (_openingFace == 0)
    23.             {
    24.                 _doorAnimator.SetTrigger("OpenInside");
    25.             }
    26.             else
    27.             {
    28.                 _doorAnimator.SetTrigger("OpenOutside");
    29.             }
    30.             _isOpenable = false;
    31.         }
    32.     }
    33.     public void OpeningDone()
    34.     {
    35.         _doorCollider.isTrigger = false;
    36.     }
    37. }
    it calls OpenDoor method with my raycast code .In server each users can open door but unfortunately their animations are not sync. When a user open a door , a door still looks closed in other player. Do you know how can I synch this doors?. Thanks again have a great day.
    Btw here is my raycast and player setup code for explain my algorithm better.
    Code (CSharp):
    1. public class PlayerSetup : MonoBehaviourPunCallbacks
    2. {
    3.     [SerializeField]
    4.     private FpsController _fpsCode = null;
    5.     [SerializeField]
    6.     private MouseController _cameraCode = null;
    7.     [SerializeField]
    8.     private SoundLogic _soundCode = null;
    9.     [SerializeField]
    10.     private Camera _cameraMain = null;
    11.     [SerializeField]
    12.     private RaycastLogic _rCastLogic = null;
    13.  
    14.     [SerializeField]
    15.     TextMeshProUGUI _playerNameText = null;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         if (photonView.IsMine)
    20.         {
    21.             _fpsCode.enabled = true;
    22.             _cameraCode.enabled = true;
    23.             _soundCode.enabled = true;
    24.             _cameraMain.enabled = true;
    25.             _rCastLogic.enabled = true;
    26.         }
    27.         else
    28.         {
    29.             _fpsCode.enabled = false;
    30.             _cameraCode.enabled = false;
    31.             _soundCode.enabled = false;
    32.             _cameraMain.enabled = false;
    33.             _rCastLogic.enabled = false;
    34.         }
    35.         SetPlayerUI();
    36.     }
    37.     public void SetPlayerUI()
    38.     {
    39.         if (_playerNameText != null)
    40.         {
    41.             _playerNameText.text = photonView.Owner.NickName;
    42.         }
    43.     }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class RaycastLogic : MonoBehaviour
    8. {
    9.     [SerializeField] private float _RaycastDistance = 0;
    10.  
    11.     [SerializeField] private Image _CrossHair = null;
    12.  
    13.     [SerializeField] private LayerMask _RaycastLayers = default;
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         GetRayCast();
    19.     }
    20.  
    21.     private void GetRayCast()
    22.     {
    23.         Vector3 _lookingDirection = transform.TransformDirection(Vector3.forward);
    24.         RaycastHit _hit;
    25.         if (Physics.Raycast(transform.position, _lookingDirection, out _hit, _RaycastDistance, _RaycastLayers))
    26.         {
    27.             if (_hit.transform.gameObject.tag == "SimpleDoor")
    28.             {
    29.                 _CrossHair.color = Color.red;
    30.                 if(Input.GetKeyDown(KeyCode.F))
    31.                 {
    32.                     _hit.transform.gameObject.GetComponent<SimpleDoor>().OpenDoor();
    33.                 }
    34.                 //_ItemText.text = "Press F To Take BoltCutter";
    35.             }
    36.             else
    37.             {
    38.                 _CrossHair.color = Color.white;
    39.             }
    40.         }
    41.         else
    42.         {
    43.             _CrossHair.color = Color.white;
    44.         }
    45.     }
    46. }
     
  2. Sound-Master

    Sound-Master

    Joined:
    Aug 1, 2017
    Posts:
    40
    Have you looked into RPCs? If I understood correctly what you want to do, you want the door to open regardless of which player opened it. In this case you can call an RPC which will be executed on all clients so to open the door for them too.
     
    Batuhan13 and Foestar like this.
  3. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    338
    Sound-Master is right, for this you need to send an RPC to all players telling them to open the door. You can do this by calling your RPC like so:
    Code (CSharp):
    1. base.photonView.RPC("RPCFunctionNameGoesHere", RpcTarget.All);
    Then create the actual function like so:
    Code (CSharp):
    1. [PunRPC]
    2.     public void TheNameOfYourRPCfromAbove()
    3.     {
    4.         //place your open door code in here
    5.     }
    RPC's are also known as "Remote Procedure Calls" and some information can even be passed through them. You can read more about them HERE!

    Note that in order for the above to work you have to derive from "MonoBehaviourPunCallbacks" and make sure you're using Photon.Pun at the top of your script.

    Happy Game Dev'ing!
     
    Batuhan13 likes this.
  4. Batuhan13

    Batuhan13

    Joined:
    Apr 9, 2017
    Posts:
    116
    @
    Thanks friends I will try =) Thanks for the showing way =)
     
    Sound-Master likes this.
unityunity