2017-03-29 12 views
0

현재 멀티 플레이에서 빛의 밝기를 변경하려고 할 때 문제가 있습니다.광도 변경 Unity 멀티 플레이어

게임을 시작한 사람, 호스트의 광도가 미세하게 변경됩니다. 그러나 호스트에 연결하는 사람들은 빛의 강도가 변하지 않습니다.

[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(); 
    } 
} 

내 문제는 빛의 강도가 한 플레이어, 호스트에 대해서만 변경된다는 것이다. 게임에 연결하는 모든 플레이어의 조명 강도를 변경하고 싶습니다. 어떤 제안이라도 환영합니다.

답변

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

작성한 else 문에서 light 오브젝트를 찾을 수 없다는 것을 의미하는 OnStartLocalPlayer() 메소드가 있기 때문에이 방법이 작동하지 않을까 걱정된다. – arjwolf

0

나는 완전히이 클래스에서 빛의 세기 변화의 논리를 제거하여이 문제를 해결할 수 있었다. 내가 앞서 간 내 SpawnManager_waveLevel에 빛의 강도를 변경할 수있는 조건을 넣어 한

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; 
    } 
} 

지금은 잘 작동 :이 클래스는 이제 다음과 같습니다. 이것이 저와 같은 문제에 직면 한 사람이 도움이되기를 바랍니다.

오와 그 클래스의 큰 변화가 OnLightAmountChange()에서 더 이상 설정되지 않았습니다. lightAmout = amount; 이제는 다음과 같이 설정할 수 있습니다. light.intensity = amount; 실제 강도입니다.