0

我有一个使用手电筒的多人游戏,如果我按下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);
        }
    }

}
4

1 回答 1

0

最优化的网络解决方案是同步数据良好且传输数据量最少的解决方案,只需在手电筒开启或关闭时通过网络发送,并在两个客户端上创建计时器,因此将在两个客户端上独立生成 flashlightRanOut,噪音也会在两个客户端上生成,闪烁会有所不同,因为它使用随机值,但不需要完全相同的闪烁(也不可能这样做,因为延迟会搞砸)。

于 2015-10-28T22:04:33.387 回答