I have 3 instances of a local host game open, on each, when I press spacebar, I want the player associated with that client to turn green, and be shown green in all the other game windows.
The following code works, but is it the correct way of doing these things?
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine;
public class test_box : NetworkBehaviour {
public override void OnStartLocalPlayer()
{
GetComponent<MeshRenderer>().material.color = Color.blue;
}
public void Update()
{
if (isLocalPlayer)
{
if (Input.GetKeyDown(KeyCode.Space))
{
CmdChangeColor(Color.green);
}
if (Input.GetKeyUp(KeyCode.Space))
{
CmdChangeColor(Color.blue);
}
}
}
[Command]
void CmdChangeColor(Color color)
{
RpcChangeColor(color);
}
[ClientRpc]
void RpcChangeColor(Color color)
{
if (color == Color.blue && !isLocalPlayer)
{
GetComponent<MeshRenderer>().material.color = Color.white;
}
else
{
GetComponent<MeshRenderer>().material.color = color;
}
}
}
seems a bit slow over matchmaker, the host will turn green before the clients even if spacebar is pressed by a client. Ideally I’m looking for a way to do this where the effect happens for all at the same time.