Question by
nunekloga · Apr 02, 2020 at 07:34 AM ·
networkingsynchronization
Hey, I don't know how to write a light switch synchronization. Can anyone help me Networking mirror Below is the script
using UnityEngine; using System.Collections; using Mirror;
public class LightSwitch : NetworkBehaviour {
public bool onSwitch;
public bool lightStatus;
public GameObject theLight;
void OnTriggerEnter(Collider other)
{
onSwitch = true;
}
void OnTriggerExit(Collider other)
{
onSwitch = false;
}
void Update()
{
if(theLight.active == true)
{
lightStatus = true;
}
else
{
lightStatus = false;
}
if (onSwitch)
{
if (lightStatus)
{
if (Input.GetKeyDown(KeyCode.E))
{
CmdON();
}
}
else
{
if (Input.GetKeyDown(KeyCode.E))
{
CmdOFF();
}
}
}
}
[Command]
void CmdOFF()
{
RpcOFF();
}
[Command]
void CmdON()
{
RpcON();
}
[ClientRpc]
void RpcON(){
theLight.active = false;
}
[ClientRpc]
void RpcOFF(){
theLight.active = true;
}
void OnGUI()
{
if (onSwitch && !isLocalPlayer)
{
if (lightStatus)
{
GUI.Box(new Rect(0, 0, 200, 20), "Naciśnij E, aby zamknąć światło");
}
else
{
GUI.Box(new Rect(0, 0, 200, 20), "Naciśnij E, aby otworzyć światło");
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
What is the best way to send data from a client to the server using Mirror? 1 Answer
uNet Network Sync Problem With Non-Player Object 0 Answers
UNET Door sync 0 Answers
Delay in synchronization in unity3d Unet 0 Answers
Network enemy spotting 0 Answers