I have a little problem with syncing booleans on my networking game.
I use the Mirror-Package for networking and use KCP for transport.
When the players basicly collide each other (using a sphere trigger box for detection) they should change their state from false to true or otherwise.
Sometimes it is working and sometimes booth get the same state displayed by color - the problem mostly accures when running.
Tested with a friend - the color get synced correctly but the booleans sometimes do not get set correctly. For debugging purposes the state can be changed by key press.
Thanks for advice!
private void OnTriggerEnter(Collider other) // Getting called a player enter collision
{
if (isLocalPlayer) // Check if this is local player
{
if (other.gameObject != gameObject) // Double check it not own body - problematic on begging
{
if (cooldown == false) // Make sure player is not on cooldown
{
if (other.gameObject.CompareTag("Player")) // Make sure the gameobject has right tag
{
ChangePlayerState(caught); // Call function an pass in current state which is a boolean
}
}
}
}
}
#region Player State
[Client]
public void ChangePlayerState(bool state)
{
cooldown = true; // Activate cooldown
if (state == true) // Do logic for true
{
caught = false;
stateLight.color = Color.green;
}
else
if (state == false) // Do logic for false
{
caught = true;
stateLight.color = Color.red;
}
Invoke("ChangeCooldown", 3);
// Networking-Part - Changing only color when networking
if (isClient) CmdChangePlayerState(caught); // Execute Command
if (isServer) RpcChangePlayerState(caught); // Execute Rpc
}
[Command]
private void CmdChangePlayerState(bool state) // Change color according to statebool
{
if (state == true)
{
stateLight.color = Color.red;
}
else
if (state == false)
{
stateLight.color = Color.green;
}
}
[ClientRpc]
private void RpcChangePlayerState(bool state) // Change color according to statebool
{
if (state == true)
{
stateLight.color = Color.red;
}
else
if (state == false)
{
stateLight.color = Color.green;
}
}