2017-12-25 1 views
0

두 개의 조명 구성 요소가 있습니다. 먼저 조명을 찾고 조명을 꺼야합니다. 그런 다음 일부 개체 배율을 변경하고 조명이 흐려지는 동안 개체 배율 지속 시간을 사용할 때이를 활성화하려고합니다.어떻게 조명 부품을 어둡게 할 수 있습니까?

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

번째 스크립트 오브젝트로 스케일링된다

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

를 ChangeScale 내가 DimLights 스크립트있어서 LightDim를 사용하여 광을 어둡게 할 scaleOverTime 방법 안에 원하는 제 스크립트.

+1

빛이 어둡게 될 때와 밝아 질 때를 언급하지 않았습니다. – Programmer

+0

@Programmer 오른쪽, 스케일 업하면 밝아지고 스케일을 줄이면 끄기 전까지 어두워 져야합니다. –

+1

배열의 모든 표시등이이 작업을 수행해야합니까? – Programmer

답변

2

그렇게 복잡하지는 않습니다. scaleOverTime 함수를 변경하여 함수를 복사하여 빛에서 작동하도록하고 함수에서 새 함수를 만듭니다. 변경해야 할 것은 Vector3.Lerp 기능이 Mathf.Lerp이며, targetObj.transform.localScale ~ targetObj.intensity 기능입니다.

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

이 새로운 코 루틴을 시작하는 것을 방지 :

간단한 빛 희미한 기능 : 함수가 배열을 취할해야한다 불행하게도

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

, 당신은 배열을 사용하는 각 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

문제 없습니다. "I"를 대문자로 생각하십시오. 우리는 [여기]에 대해 이야기했습니다 (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

관련 문제