随着时间的推移来回改变光强度
Change light intensity back and forth over time
如何在 2 秒后将光强值从 3.08 改回 1.0。我在我的代码中有评论以获取更多信息
public class Point_LightG : MonoBehaviour {
public Light point_light;
float timer;
// Use this for initialization
void Start () {
point_light = GetComponent<Light>();
}
// Update is called once per frame
void Update () {
timer -= Time.deltaTime;
lights();
}
public void lights()
{
if (timer <= 0)
{
point_light.intensity = Mathf.Lerp(1.0f, 3.08f, Time.time);
timer = 2f;
}
// so after my light intensity reach 3.08 I need it to gradually change back to 1.0 after 2 seconds.
}
}
要在两个值之间进行插补,只需使用 Mathf.PingPong
和 Mathf.Lerp
并提供插补应该发生的速度。
public Light point_light;
public float speed = 0.36f;
float intensity1 = 3.08f;
float intensity2 = 1.0f;
void Start()
{
point_light = GetComponent<Light>();
}
void Update()
{
//PingPong between 0 and 1
float time = Mathf.PingPong(Time.time * speed, 1);
point_light.intensity = Mathf.Lerp(intensity1, intensity2, time);
}
如果您更喜欢使用 duration 而不是 speed 变量来控制光强度,那么您最好使用协程函数和带有简单计时器的 Mathf.Lerp
函数。然后可以在 x 秒内完成 lerp。
IEnumerator LerpLightRepeat()
{
while (true)
{
//Lerp to intensity1
yield return LerpLight(point_light, intensity1, 2f);
//Lerp to intensity2
yield return LerpLight(point_light, intensity2, 2f);
}
}
IEnumerator LerpLight(Light targetLight, float toIntensity, float duration)
{
float currentIntensity = targetLight.intensity;
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
targetLight.intensity = Mathf.Lerp(currentIntensity, toIntensity, counter / duration);
yield return null;
}
}
用法
public Light point_light;
float intensity1 = 3.08f;
float intensity2 = 1.0f;
void Start()
{
point_light = GetComponent<Light>();
StartCoroutine(LerpLightRepeat());
}
如何在 2 秒后将光强值从 3.08 改回 1.0。我在我的代码中有评论以获取更多信息
public class Point_LightG : MonoBehaviour {
public Light point_light;
float timer;
// Use this for initialization
void Start () {
point_light = GetComponent<Light>();
}
// Update is called once per frame
void Update () {
timer -= Time.deltaTime;
lights();
}
public void lights()
{
if (timer <= 0)
{
point_light.intensity = Mathf.Lerp(1.0f, 3.08f, Time.time);
timer = 2f;
}
// so after my light intensity reach 3.08 I need it to gradually change back to 1.0 after 2 seconds.
}
}
要在两个值之间进行插补,只需使用 Mathf.PingPong
和 Mathf.Lerp
并提供插补应该发生的速度。
public Light point_light;
public float speed = 0.36f;
float intensity1 = 3.08f;
float intensity2 = 1.0f;
void Start()
{
point_light = GetComponent<Light>();
}
void Update()
{
//PingPong between 0 and 1
float time = Mathf.PingPong(Time.time * speed, 1);
point_light.intensity = Mathf.Lerp(intensity1, intensity2, time);
}
如果您更喜欢使用 duration 而不是 speed 变量来控制光强度,那么您最好使用协程函数和带有简单计时器的 Mathf.Lerp
函数。然后可以在 x 秒内完成 lerp。
IEnumerator LerpLightRepeat()
{
while (true)
{
//Lerp to intensity1
yield return LerpLight(point_light, intensity1, 2f);
//Lerp to intensity2
yield return LerpLight(point_light, intensity2, 2f);
}
}
IEnumerator LerpLight(Light targetLight, float toIntensity, float duration)
{
float currentIntensity = targetLight.intensity;
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
targetLight.intensity = Mathf.Lerp(currentIntensity, toIntensity, counter / duration);
yield return null;
}
}
用法
public Light point_light;
float intensity1 = 3.08f;
float intensity2 = 1.0f;
void Start()
{
point_light = GetComponent<Light>();
StartCoroutine(LerpLightRepeat());
}