2017-12-25 13 views
0

私は2つの照明コンポーネントを持っています。まず、私は両方のライトを見つけて、それらを無効にしています。 次に、いくつかのオブジェクトスケールを変更し、ライトディメンションのオブジェクトスケーリング継続時間を使用する際にそれらを有効にしたいと思います。どのように光成分を薄暗くすることができますか?

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class DimLights : MonoBehaviour 
{ 
    //Lights Change 
    public Light[] lightsToDim = null; 
    public float maxTime; 

    private GameObject[] myLights; 
    private float mEndTime = 0; 
    private float mStartTime = 0; 

    private void Start() 
    { 
     myLights = GameObject.FindGameObjectsWithTag("Light"); 
     mStartTime = Time.time; 
     mEndTime = mStartTime + maxTime; 
     LightsState(false); 
    } 

    public void LightsState(bool state) 
    { 
     foreach (GameObject go in myLights) 
     { 
      go.GetComponent<Light>().enabled = state; 
     } 
    } 

    public void LightDim() 
    { 
     foreach (Light light in lightsToDim) 
     { 
      light.intensity = Mathf.InverseLerp(mStartTime, mEndTime, Time.time); 
     } 
    } 
} 

2番目のスクリプトは、いくつかのオブジェクトをスケーリングされる:第二スクリプトで

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class ChangeScale : MonoBehaviour 
{ 
    //Scaling change 
    public GameObject objectToScale; 
    public float duration = 1f; 
    public Vector3 minSize; 
    public Vector3 maxSize; 

    private bool scaleUp = false; 
    private Coroutine scaleCoroutine; 

    //Colors change 
    public Color startColor; 
    public Color endColor; 
    public float colorDuration; // duration in seconds 

    private void Start() 
    { 
     startColor = GetComponent<Renderer>().material.color; 
     endColor = Color.green; 
     objectToScale.transform.localScale = minSize; 
    } 

    // Use this for initialization 
    void Update() 
    { 
     if (Input.GetKeyDown(KeyCode.F)) 
     { 
      //Flip the scale direction when F key is pressed 
      scaleUp = !scaleUp; 

      //Stop old coroutine 
      if (scaleCoroutine != null) 
       StopCoroutine(scaleCoroutine); 

      //Scale up 
      if (scaleUp) 
      { 
       //Start new coroutine and scale up within 5 seconds and return the coroutine reference 
       scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, maxSize, duration)); 
      } 

      //Scale Down 
      else 
      { 
       //Start new coroutine and scale up within 5 seconds and return the coroutine reference 
       scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, minSize, duration)); 
      } 
     } 

     if (Input.GetKeyDown(KeyCode.C)) 
     { 
      StartCoroutine(ChangeColor()); 
     } 
    } 

    IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration) 
    { 
     float counter = 0; 

     //Get the current scale of the object to be scaled 
     Vector3 startScaleSize = targetObj.transform.localScale; 

     while (counter < duration) 
     { 
      counter += Time.deltaTime; 
      targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter/duration); 
      yield return null; 
     } 
    } 

    IEnumerator ChangeColor() 
    { 
     float t = 0; 

     while (t < colorDuration) 
     { 
      t += Time.deltaTime; 
      GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, t/colorDuration); 
      yield return null; 
     } 
    } 
} 

iはDimLightsスクリプト方法LightDimを用いて光を調光するscaleOverTimeメソッド内たいChangeScale。

+1

光が暗くなるときと明るくなるときは、決して言及していません。 – Programmer

+0

@Programmer右、スケールアップすると明るくなり、スケールが小さくなると消灯するまで暗くなります。 –

+1

アレイのすべてのライトがこれを行う必要がありますか? – Programmer

答えて

2

それほど複雑ではありません。 scaleOverTime関数を変更して、関数をコピーして新しい関数を作成することで、ライト上で動作させます。変更する唯一のことは、Vector3.Lerpの機能をMathf.Lerpに機能させ、targetObj.transform.localScaletargetObj.intensityにすることです。

シンプルなライト薄暗い機能:

IEnumerator dimLightOverTime(Light targetObj, float toIntensity, float duration) 
{ 
    float counter = 0; 

    //Get the current intensity of the Light 
    float startIntensity = targetObj.intensity; 

    while (counter < duration) 
    { 
     counter += Time.deltaTime; 
     targetObj.intensity = Mathf.Lerp(startIntensity, toIntensity, counter/duration); 
     yield return null; 
    } 
} 

関数は、配列を取るためになされるべきであるので、残念ながら、あなたは、配列を使用している:

IEnumerator dimLightOverTime(Light[] targetObj, float toIntensity, float duration) 
{ 
    float counter = 0; 
    //Get the current intensity of the Light 
    float[] startIntensity = new float[targetObj.Length]; 
    for (int i = 0; i < targetObj.Length; i++) 
    { 
     startIntensity[i] = targetObj[i].intensity; 
    } 

    while (counter < duration) 
    { 
     counter += Time.deltaTime; 

     for (int i = 0; i < targetObj.Length; i++) 
     { 
      targetObj[i].intensity = Mathf.Lerp(startIntensity[i], toIntensity, counter/duration); 
     } 
     yield return null; 
    } 
} 

これはのための新しいコルーチンを開始する必要がないようにそれぞれLightと時間を節約します。

新しいUpdate機能:

public Light[] lightsToDim = null; 
private Coroutine lightCoroutine; 

// Use this for initialization 
void Update() 
{ 
    if (Input.GetKeyDown(KeyCode.F)) 
    { 
     //Flip the scale direction when F key is pressed 
     scaleUp = !scaleUp; 

     //Stop old coroutine 
     if (scaleCoroutine != null) 
      StopCoroutine(scaleCoroutine); 

     if (lightCoroutine != null) 
      StopCoroutine(lightCoroutine); 


     //Scale up 
     if (scaleUp) 
     { 
      //Start new coroutine and scale up within 5 seconds and return the coroutine reference 
      scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, maxSize, duration)); 
      lightCoroutine = StartCoroutine(dimLightOverTime(lightsToDim, 1, duration)); ; 
     } 

     //Scale Down 
     else 
     { 
      //Start new coroutine and scale up within 5 seconds and return the coroutine reference 
      scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, minSize, duration)); 
      lightCoroutine = StartCoroutine(dimLightOverTime(lightsToDim, 0, duration)); ; 
     } 
    } 
} 

お知らせ新しい変数 "lightCoroutine"。それは私たちがscaleCoroutineのためにしたのと同じように古いコルーチンを格納するために使用されます。

+1

問題ありません。 「私」を大文字にしてください。私たちはそれについて話しました[ここ](https://stackoverflow.com/questions/47958954/how-can-i-make-that-when-pressing-f-fast-many-time-it-will-scale-the-オブジェクトダウン#comment82887150_47958954)。まだ混乱している場合は、[this](https://english.stackexchange.com/a/33116)を参照してください。 – Programmer

関連する問題