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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class PlayerDoor : NetworkBehaviour
{
private GameObject frontDoorCollider;
private GameObject backDoorCollider;
private GameObject sideDoorCollider;
private Transform door;
private Transform doorAfterRaycast;
public float ySensitivity = 1500f;
public float frontOpenPosLimit = 90f;
public float backOpenPosLimit = 90;
private float currentPos;
private float positive = 0;
private float negative = 0;
bool moveDoor = false;
DoorCollision doorCollision = DoorCollision.NONE;
// Start is called before the first frame update
void Start()
{
door = GameObject.FindWithTag("Door").transform;
frontDoorCollider = door.Find("Front").gameObject;
backDoorCollider = door.Find("Back").gameObject;
sideDoorCollider = door.Find("Side").gameObject;
}
// Update is called once per frame
void Update()
{
//if (!isLocalPlayer) return;
if (Input.GetMouseButtonDown(0))
{
if(isServer){
RpcMoveDoor();
}else{
if (hasAuthority) {
CmdMoveDoor();
}
}
}
if (Input.GetMouseButtonUp(0))
{
moveDoor = false;
}
}
void OpenDoor(){
//Debug.Log("Here");
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo))
{
doorAfterRaycast = hitInfo.collider.transform.parent;
Debug.Log(doorAfterRaycast);
if (hitInfo.collider.gameObject.name == "Front" || hitInfo.collider.gameObject.name == "Side")
{
moveDoor = true;
doorCollision = DoorCollision.FRONT;
}
else if (hitInfo.collider.gameObject.name == "Back")
{
moveDoor = true;
doorCollision = DoorCollision.BACK;
// JUST FOR TESTING
float random = Random.Range(0, 90.0f);
doorAfterRaycast.localRotation = Quaternion.Euler(0, random, 0);
// END JUST FOR TESTING
}
else
{
doorCollision = DoorCollision.NONE;
}
}
return;
}
IEnumerator doorMover()
{
bool stoppedBefore = false;
float yRot = 0;
while (true)
{
if (moveDoor)
{
stoppedBefore = false;
yRot += Input.GetAxis("Mouse Y") * ySensitivity * Time.deltaTime;
if (doorCollision == DoorCollision.FRONT)
{
if(negative == 0){
negative++;
positive = 0;
yRot = -yRot;
}
yRot = Mathf.Clamp(yRot, -frontOpenPosLimit, 0);
//doorAfterRaycast.localRotation = Quaternion.Euler(0, -yRot, 0);
}
else if (doorCollision == DoorCollision.BACK)
{
if(positive == 0){
positive++;
negative = 0;
yRot = Mathf.Abs(yRot);
}
yRot = Mathf.Clamp(yRot, 0, backOpenPosLimit);
//doorAfterRaycast.localRotation = Quaternion.Euler(0, yRot, 0);
}
}
else
{
if (!stoppedBefore)
{
stoppedBefore = true;
}
}
yield return null;
}
}
enum DoorCollision
{
NONE, FRONT, BACK
}
[Command]
public void CmdMoveDoor(){
RpcMoveDoor();
}
[ClientRpc]
public void RpcMoveDoor(){
StartCoroutine(doorMover());
OpenDoor();
}
}
These are the attributes for the door
If more information needs to be provided I will do it without problem.