我正在进行日/夜循环,并且在Time.time上有lerp,因为如果我使用Time.deltaTime,它会在晚上12点左右转,所以当天早上12点。不过我离题了。 我现在遇到的问题是,在任何时间设置下,它都会使lerp成为瞬间而不是 - 好吧......'lerp'。有什么想法解决这个问题?我是C#newbie
我使用它作为时间脚本:
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;
}
}
}
这是lerp脚本:
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);
}
}
}
}
答案 0 :(得分:1)
你可以写这样的东西,Lerp取实际(从)值,最终(到)值和分数值。 (此处Time.deltaTime)现在,当您的计时器到达18小时时,您的颜色将在几个更新函数调用中更改为夜晚的颜色(您可以通过乘以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);
}
}
答案 1 :(得分:1)
与时间一起使用时,我很难理解lerp。 也许这个例子可以帮助某人:
// 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);
}
注意:您需要为:
添加一些逻辑