Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Host doesn't receive the changes clients make

Discussion in 'Multiplayer' started by hornx1806, Apr 3, 2021.

  1. hornx1806

    hornx1806

    Joined:
    Mar 4, 2021
    Posts:
    2
    I am currently working on door script, this means everyone can open the door, BUT only when the host opens the door clients see the change. When clients open the door, host and other clients don't see the change also.

    This script is attached to the player.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5.  
    6. public class PlayerDoor : NetworkBehaviour
    7. {
    8.     private GameObject frontDoorCollider;
    9.     private GameObject backDoorCollider;
    10.     private GameObject sideDoorCollider;
    11.     private Transform door;
    12.     private Transform doorAfterRaycast;
    13.  
    14.     public float ySensitivity = 1500f;
    15.     public float frontOpenPosLimit = 90f;
    16.     public float backOpenPosLimit = 90;
    17.  
    18.     private float currentPos;
    19.     private float positive = 0;
    20.     private float negative = 0;
    21.  
    22.     bool moveDoor = false;
    23.     DoorCollision doorCollision = DoorCollision.NONE;
    24.  
    25.     // Start is called before the first frame update
    26.     void Start()
    27.     {
    28.         door = GameObject.FindWithTag("Door").transform;
    29.         frontDoorCollider = door.Find("Front").gameObject;
    30.         backDoorCollider = door.Find("Back").gameObject;
    31.         sideDoorCollider = door.Find("Side").gameObject;
    32.      
    33.          
    34.            
    35.     }
    36.  
    37.     // Update is called once per frame
    38.     void Update()
    39.     {
    40.         //if (!isLocalPlayer) return;
    41.          if (Input.GetMouseButtonDown(0))
    42.         {
    43.             if(isServer){
    44.                 RpcMoveDoor();
    45.             }else{
    46.                 if (hasAuthority) {
    47.                     CmdMoveDoor();
    48.                 }
    49.             }
    50.          
    51.          
    52.         }
    53.  
    54.         if (Input.GetMouseButtonUp(0))
    55.         {
    56.             moveDoor = false;
    57.         }    
    58.     }
    59.  
    60.     void OpenDoor(){
    61. //Debug.Log("Here");
    62.         RaycastHit hitInfo = new RaycastHit();
    63.         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo))
    64.         {
    65.             doorAfterRaycast = hitInfo.collider.transform.parent;
    66.             Debug.Log(doorAfterRaycast);
    67.             if (hitInfo.collider.gameObject.name == "Front" || hitInfo.collider.gameObject.name == "Side")
    68.             {
    69.              
    70.                 moveDoor = true;
    71.                 doorCollision = DoorCollision.FRONT;
    72.             }
    73.             else if (hitInfo.collider.gameObject.name == "Back")
    74.             {
    75.                 moveDoor = true;
    76.                 doorCollision = DoorCollision.BACK;
    77. // JUST FOR TESTING
    78.                                                 float random = Random.Range(0, 90.0f);
    79.             doorAfterRaycast.localRotation = Quaternion.Euler(0, random, 0);
    80. // END JUST FOR TESTING
    81.             }
    82.             else
    83.             {
    84.                 doorCollision = DoorCollision.NONE;
    85.             }
    86.         }
    87.         return;    
    88.     }
    89.  
    90.  
    91.  
    92.     IEnumerator doorMover()
    93.     {
    94.         bool stoppedBefore = false;
    95.         float yRot = 0;
    96.      
    97.         while (true)
    98.         {
    99.          
    100.             if (moveDoor)
    101.             {
    102.              
    103.                 stoppedBefore = false;
    104.                 yRot += Input.GetAxis("Mouse Y") * ySensitivity * Time.deltaTime;
    105.                 if (doorCollision == DoorCollision.FRONT)
    106.                 {
    107.                     if(negative == 0){
    108.                         negative++;
    109.                         positive = 0;
    110.                      
    111.                         yRot = -yRot;
    112.                     }
    113.                     yRot = Mathf.Clamp(yRot, -frontOpenPosLimit, 0);
    114.                     //doorAfterRaycast.localRotation = Quaternion.Euler(0, -yRot, 0);
    115.                 }
    116.                 else if (doorCollision == DoorCollision.BACK)
    117.                 {
    118.                
    119.                     if(positive == 0){
    120.                         positive++;
    121.                         negative = 0;
    122.                         yRot = Mathf.Abs(yRot);
    123.                     }
    124.                  
    125.                     yRot = Mathf.Clamp(yRot, 0, backOpenPosLimit);
    126.                     //doorAfterRaycast.localRotation = Quaternion.Euler(0, yRot, 0);
    127.                 }
    128.             }
    129.             else
    130.             {
    131.                 if (!stoppedBefore)
    132.                 {
    133.                     stoppedBefore = true;
    134.                 }
    135.             }
    136.  
    137.             yield return null;
    138.         }
    139.  
    140.     }
    141.  
    142.  
    143.     enum DoorCollision
    144.     {
    145.         NONE, FRONT, BACK
    146.     }  
    147.  
    148.     [Command]
    149.     public void CmdMoveDoor(){
    150.  
    151.             RpcMoveDoor();
    152.      
    153.     }
    154.  
    155.     [ClientRpc]
    156.     public void RpcMoveDoor(){
    157.      
    158.         StartCoroutine(doorMover());
    159.         OpenDoor();
    160.  
    161.     }  
    162.  
    163. }
    164.  
    These are the attributes for the door
    upload_2021-4-3_9-44-47.png

    If more information needs to be provided I will do it without problem.
     
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,329
    on my phone atm but this looks almost too complicated. just use a SyncVar bool open.
    feed it to animator to play the animation.
    players use a Cmd to ask server to open the door.

    i have this script in my usurvival asset. can copy it in here later if you need it
     
  3. hornx1806

    hornx1806

    Joined:
    Mar 4, 2021
    Posts:
    2
    My current script opens the door with mouse drag. Can I use SyncVar with it?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Are you getting an error on the client that the client lacks authority to call a Command on the object, whenever the client calls CmdMoveDoor? That's what I expect to happen with a quick look at the code.
    Your method for getting input from the player should have nothing to do with the networking part of this.