Hi, I am making a multiplayer car game - and I got the multiplayer thing set up (for now). Cars are updating on each client and things are going well. However, I can’t seem to get the lights of the car to work on the other’s screen. Turning on lights are optional (plus brake lights), but these are never activated for the others. Any way to transform their position and ActiveSelf to the other players?
You can use a [Command] to [SyncVar(hook=“UpdateLights”)]/[ClientRPC] to turn on and off lights on the other clients.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class LightsControl : MonoBehaviour {
// Call a command to do an action on the server
[Command]
public void CmdUpdateLights(bool newLightActive) {
// Updating the value of lightsActive will call the hook on each client...
lightsActive = newLightActive;
// ...or you can call a ClientRpc to make the changes on the clients instead
RpcUpdateLights(newLightActive);
}
[SyncVar(hook = "SyncLights")]
public bool lightsActive;
public void SyncLights(bool value) {
lightsActive = value;
// Turn on or off lights based on lightsActive value
}
[ClientRpc]
public void RpcUpdateLights(bool activateLights) {
// Turn on or off lights based on activateLights value
}
}
1 Like