I’m trying to synchronize doors across the server for a multiplayer game using Unity and Mirror. The doors currently do synchronize across the server, however I do have a couple problems:
**If player 1 points at door 1 and player 2 at door 2, and one of the players presses OpenDoorKey, both doors will open.
If player 1 and player 2 point at door 1, and one of the players presses OpenDoorKey, the door will open and immediately close again.
**
Since I’m using raycasts in my door script, it should have to do with that. I have no idea how to fix this. Here’s the code:
private void Update()
{
if (isClient)
{
if (SceneManager.GetActiveScene().name == "Game")
{
crosshair = CrossHair.instance.crosshairImage;
}
RaycastHit hit;
Vector3 fwd = playerCamera.TransformDirection(Vector3.forward);
int mask = 1 << LayerMask.NameToLayer(excludeLayerName) | doorLayer.value;
if (Physics.Raycast(playerCamera.position, fwd, out hit, rayLength, mask))
{
if (hit.collider.CompareTag(doorTag))
{
if (!doOnce)
{
CrosshairChange(true);
}
isCrosshairActive = true;
doOnce = true;
if (Input.GetKeyDown(openDoorKey))
{
doorIdentity = hit.collider.GetComponent<NetworkIdentity>();
CmdDoorController(doorIdentity);
}
}
}
else
{
if (isCrosshairActive)
{
CrosshairChange(false);
doOnce = false;
}
}
}
}
[Command(requiresAuthority = false)]
private void CmdDoorController(NetworkIdentity doorIdentity)
{
var doorController = doorIdentity.GetComponent<DoorController>();
if (doorController != null)
{
doorController.PlayAnimation();
}
}
Just like you can see in the code, I assign the doorIdentity variable locally only if openDoorKey is pressed, thinking this would make it work, which it didn’t.
If you need anything else, please do ask. Thanks in advance.