2017-03-29 7 views
0

現在、マルチプレイヤーで自分の光の強度を変更しようとしているときに問題が発生しています。光度を変更するUnity Multiplayer

ゲームを開始した人、つまりホストの光の強さがうまく変わります。しかし、ホストに接続する人々は、その光度は変化しません。

[SyncVar]を使用して光度を変更しようとしていますが、ホストに接続しているプレーヤーが光度の変化をまったく見ていません。私のコードは次のとおりです:

using UnityEngine; 
using System.Collections; 
using UnityEngine.Networking; 

public class dayNightCycle : NetworkBehaviour { //changes day and night based on the wavelevel SpawnManager_waveLevel.cs script 

    Light light; 
    float fadeTime = 1f; 
    [SyncVar(hook = "OnLightAmountChange")] 
    float lightAmout = 0f; 
    SpawnManager_waveLevel level; 

    public override void OnStartLocalPlayer() 
    { 
     light = GetComponentInChildren<Light>(); 
     level = GetComponent<SpawnManager_waveLevel>(); 
     light.intensity = lightAmout; 
    } 

    // Update is called once per frame 
    void Update() { 

     changeLight(); 
    } 

    void changeLight() 
    { 
     if (isLocalPlayer) 
     { 
      if (level.waveCounter == 1) 
      { 
       lightAmout = 0.03f; 
       light.intensity = Mathf.Lerp(light.intensity, lightAmout, fadeTime * Time.deltaTime); 
      } 
      else 
      { 
       lightAmout = 1f; 
       light.intensity = Mathf.Lerp(light.intensity, lightAmout, fadeTime * Time.deltaTime); 
      } 
     } 
    } 

    void OnLightAmountChange(float amount) 
    { 
     lightAmout = amount; 
     changeLight(); 
    } 
} 

私の問題は、光の強さは1人のプレーヤー、ホストのためだけに変化していることです。私は光の強さがゲームに接続するすべてのプレーヤーのために変わることを望む。どんな提案も大歓迎です。

答えて

0

非常に簡単な修正 - 'isLocalPlayer'がfalseの場合、スクリプトに代替手段を提供していません。ここで

はあなたのための固定された光の変化です:

void changeLight() 
{ 
    if (isLocalPlayer) 
    { 
     if (level.waveCounter == 1) 
     { 
      lightAmout = 0.03f; 
      light.intensity = Mathf.Lerp(light.intensity, lightAmout, fadeTime * Time.deltaTime); 
     } 
     else 
     { 
      lightAmout = 1f; 
      light.intensity = Mathf.Lerp(light.intensity, lightAmout, fadeTime * Time.deltaTime); 
     } 
    } 
    else{ 
     // If not a local player, simply update to the new light intensity. 
     light.intensity = Mathf.Lerp(light.intensity, lightAmout, fadeTime * Time.deltaTime); 
    } 
} 
+0

私はOnStartLocalPlayer()メソッドを持っているので、これはうまくいきません。これは、作成したelseステートメントがライトオブジェクトを見つけられないことを意味します。 – arjwolf

0

私は完全にこのクラスから光強度変化のロジックを削除することによってこの問題を解決することができました。このクラスは現在、次のようになります。

using UnityEngine; 
using System.Collections; 
using UnityEngine.Networking; 

public class dayNightCycle : NetworkBehaviour { //changes day and night based on the wavelevel SpawnManager_waveLevel.cs script 

    [SerializeField] 
    public Light light; 

    [SerializeField] 
    SpawnManager_waveLevel level; 

    [SyncVar(hook = "OnLightAmountChange")] 
    public float lightAmout = 0f; 

    public override void OnStartLocalPlayer() 
    { 
     OnLightAmountChange(lightAmout); 
    } 

    void OnLightAmountChange(float amount) 
    { 
     light.intensity = amount; 
    } 
} 

私は先に行って、私のSpawnManager_waveLevelに光の強度を変更するために条件を入れて、今では正常に動作しました。私はこれが私と同じ問題に直面している人を助けることを願っています。

ああ、そのクラスの大きな変更はOnLightAmountChange()メソッドにありましたが、私はもはや設定しませんlightAmout = amount;表示されるように次のように設定しましたlight.intensity = amount;実際の強度。

関連する問題