【问题标题】:Increase Light spotAngle overtime增加光斑角度超时
【发布时间】:2018-02-01 11:51:58
【问题描述】:

我有一个小问题。我想在一段时间内增加Light.spotAngle 属性。编写的代码有效,但我希望随着速度的增加,或类似的东西。我想以某种速度增加光斑角度的值,不是直接100,而是从30慢慢增长10到100。

Transform thisLight = lightOB.transform.GetChild(0);
Light spotA = thisLight.GetComponent<Light>();
spotA.spotAngle = 100f;

我尝试使用 Time.DeltaTime,但不起作用。 救命!!!

【问题讨论】:

  • 您的问题不完整。从哪里增加到哪里?什么时候开始增加,什么时候停止?
  • 嗯,现在在我的光斑角度是 30,我需要达到 100
  • 然后改成100..有什么问题?
  • 当我点击一个对象时,这个索引需要慢慢增长到100
  • 我从您的最后 2 条评论中了解到。只需将其编辑为您的问题,以便想要帮助您的人可以阅读它,而不是滚动评论以找到实际问题。还要添加对您不起作用的带有“Time.DeltaTime”的代码。也许有人可以弄清楚为什么它不起作用。

标签: c# unity3d light


【解决方案1】:

使用Mathf.Lerp 将值a 转换为b。根据您的问题,a 值为10,b 值为100。在协程函数中执行此操作。这使您可以控制希望这种缓慢的转换发生多长时间。 当你点击一个对象时,启动协程函数。

协程函数:

IEnumerator increaseSpotAngle(Light lightToFade, float a, float b, float duration)
{
    float counter = 0f;

    while (counter < duration)
    {
        counter += Time.deltaTime;

        lightToFade.spotAngle = Mathf.Lerp(a, b, counter / duration);

        yield return null;
    }
}

将在 5 秒内将 Light 的 SpotAngle 从 10 更改为 100。

public Light targetLight;

void Start()
{
    StartCoroutine(increaseSpotAngle(targetLight, 30, 100, 5));
}

请参阅this 帖子了解如何检测对任何游戏对象的点击。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 2019-04-08
    • 2015-04-28
    • 2017-09-16
    相关资源
    最近更新 更多