简体   繁体   中英

Time.time make my Color Lerp not 'lerp'

I'm making a day/night cycle and have the lerp on Time.time, because if I use Time.deltaTime it turns night.day around so its day at 12am. However I digress. The problem I'm running into now too is that with any Time setting it will make the lerp instant and not - well.. 'lerp'. Any idea on fixing this? I'm a C# newbie

I got it working with this as the time script:

using UnityEngine;
using System.Collections;

public class timeFlow : MonoBehaviour 
{

public float Hours = 00;
public float Minutes = 00;

void Update()
{
if(Hours <= 23){

    if(Minutes >= 60)
    {
        Minutes = 0;
        if(Minutes <= 59)
        {
            Hours++;
        }
        else
        {
            Minutes = 0;
            Hours = 0;
            guiText.text = Hours.ToString("f0") + ":0" +    Minutes.ToString("f0");
        }
    }
    else
    {
        Minutes += UnityEngine.Time.deltaTime * 100;
    }

    if(Mathf.Round(Minutes) <= 9)
    {
        guiText.text = Hours.ToString("f0") + ":0" +    Minutes.ToString("f0");
    }
    else
    {
        guiText.text = Hours.ToString("f0") + ":" + Minutes.ToString("f0");
    }
}

else {
    Hours = 0;
    }
}
}

And this is the lerp script:

using UnityEngine;
using System.Collections;

public class cycleFlow : MonoBehaviour {

public Color32 night = new Color32(30, 30, 30, 255);
public Color32 day = new Color32(255, 255, 255, 255);

public GameObject Timer;
private timeFlow TimeFlow;

void Awake () {

    TimeFlow = Timer.GetComponent<timeFlow> ();

}

void Update () {
    DayNightCycle ();
}

void DayNightCycle()
{

    foreach (SpriteRenderer child in    transform.GetComponentsInChildren<SpriteRenderer>()) {
        if (TimeFlow.Hours == 18){
            child.color = Color.Lerp(day, night, Time.time);
        }

        if (TimeFlow.Hours == 6) {
            child.color = Color.Lerp(night, day, Time.time);
        }
    }
}
}

You can write something like this, Lerp takes actual (from) value, final (to) value and fraction value. (here Time.deltaTime) Now when your timer will reach hour 18, your Color will change to color of night in few Update function calls (you can control change time by multiplying Time.deltaTime) :)

foreach ( SpriteRenderer child in transform.GetComponentsInChildren<SpriteRenderer>() ) {
    if ( TimeFlow.Hours == 18 ) {
        child.color = Color.Lerp(child.color, night, Time.deltaTime);
        // here two times faster
        // child.color = Color.Lerp(child.color, night, Time.deltaTime * 2.0f);
    }

    if ( TimeFlow.Hours == 6 ) {
        child.color = Color.Lerp(child.color, day, Time.deltaTime);
        // here half slower :)
        // child.color = Color.Lerp(child.color, night, Time.deltaTime * 0.5f);
    }
}

I had a hard time understanding lerp when used together with a time. Maybe this example helps someone:

// lerp values between 0 and 10 in a duration of 3 seconds:
private float minValue = 0.0f;
private float maxValue = 10.0f;

private float totalDuration = 3.0f; 
private float timePassed = 0.0f;

private float currentValue;

void Update()
{
    timePassed += Time.deltaTime;

    // Lerp expects a value between 0 and 1 as the third parameter,
    // so we need to divide by the duration:
    currentValue = Mathf.Lerp(minValue, maxValue, timePassed/totalDuration);
}

Note: you need to add some logic for:

  • handling when to start/stop lerping
  • when to reset timePassed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM