我有一个使用手电筒的多人游戏,如果我按下L
手电筒,其他游戏也会关闭,但是当batteryTimeLeft
达到 20 时,它只会在本地游戏上闪烁。
如何将整个脚本与所有用户同步,以便闪烁也通过网络?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LightManager : Photon.MonoBehaviour {
public GameObject flashlight;
public Light lightinflashlight;
public bool Lightenabled = true;
public float batteryTimeLeft = 100f;
public float batteryDrainRate;
public float minIntensity = 0.25f;
public float maxIntensity = 8f;
public bool flashlightRanOut = false;
float random;
float noise;
public float time=3;
void start(){
PhotonView photonView = this.photonView;
lightinflashlight = GetComponentInChildren<Light> ();
}
void Update () {
photonView.RPC("publicUpdate", PhotonTargets.All);
}
void FixedUpdate() {
photonView.RPC("publicFixedUpdate", PhotonTargets.All);
}
[PunRPC]
public void publicFixedUpdate(){
batteryTimeLeft -= batteryDrainRate;
if (time < 0) {
time = 0;
}
time--;
}
[PunRPC]
public void publicUpdate(){
if (batteryTimeLeft >= 20) {
flashlight.GetComponent<Light> ().intensity = 8;
flashlightRanOut = false;
}
if (batteryTimeLeft < 0) {
batteryTimeLeft = 0;
flashlightRanOut=true;
}
if (!flashlightRanOut) {
if (batteryTimeLeft <= 20) {
if (time > 1) {
random = Random.Range (0.0f, 150.0f);
noise = Mathf.PerlinNoise (random, Time.time);
flashlight.GetComponent<Light> ().intensity = Mathf.Lerp (minIntensity, maxIntensity, noise);
}
}
if (time < 0) {
time = 3;
}
if (Input.GetKeyDown (KeyCode.L)) {
Lightenabled = ! Lightenabled;
}
flashlight.SetActive (Lightenabled);
} else {
flashlight.SetActive (false);
}
}
}