【问题标题】:How to I make this script stay in day and night longer?如何让这个脚本在白天和黑夜中停留更长时间?
【发布时间】:2017-05-03 06:39:23
【问题描述】:

我不太明白这段代码,所以我很难修改它。

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

public class SunScript : MonoBehaviour {
    public float duration = 1.0F;
    public Light lt;
    void Start() {
        lt = GetComponent<Light>();
    }
    void Update() {
        float phi = Time.time / duration * 0.1f * Mathf.PI;
        float amplitude = Mathf.Cos(phi) * 0.5F + 0.5F;
        lt.intensity = amplitude;
    }
}

这会在一个循环中上下移动光的强度。但是,我想让它在最亮和最暗的状态下保持一段时间,然后再开始向另一个方向移动。我该怎么做?

【问题讨论】:

  • 你不能只增加持续时间变量吗?
  • 找到一些在线数学绘图仪,例如this,并使用 Time.time 作为 x 参数输入您的数学方程。然后根据需要修改方程式。
  • @Louis-Roch Tessier 我只想增加最亮和最暗的部分,而不是介于两者之间的时间。持续时间控制整个周期。

标签: c# unity3d unity5 lighting light


【解决方案1】:

如何替换Mathf.Cos(phi) 之一these 函数而不是余弦?

使用该页面底部的公式:

float amplitude = Mathf.Sin(Mathf.PI / 2f * Mathf.Cos(phi)) * 0.5f + 0.5f;

对于带有 b 项的公式,您可以这样做(使用额外的临时变量使其更具可读性)。

float b = // whatever your b parameter is or have this declared as a class field that you can set in the Unity editor
float cosine = Mathf.Cos(phi);
float numerator = 1f + (b * b);
float denominator = 1f + (b * b * cosine * cosine);
float amplitude = (Mathf.Sqrt(numerator / denominator) * cosine) * 0.5f + 0.5f;

【讨论】:

  • 这将使从白天到夜晚/从夜晚到白天的过渡更快。
  • 这比我的效果更好,但上面的图表(包含所有 b^2 内容的图表)看起来更好。我如何将该等式转换为 C# 代码?
  • 我在答案中添加了 b 方程。
【解决方案2】:

您也可以使用 Unity AnimationCurve 来实现:

public class SunScript : MonoBehaviour
{
    public float duration = 1.0F;
    public AnimationCurve curve;

    private Light lt;

    void Start()
    {
        lt = GetComponent<Light>();
    }

    void Update()
    {
        lt.intensity = curve.Evaluate((Time.time % duration) / duration);
    }
}

只需确保您的曲线在 X 轴上从 0 到 1 并使其在 Y 轴上成为您想要的值。

希望对你有帮助,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2018-02-15
    相关资源
    最近更新 更多