首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单位填充量lerp

单位填充量lerp
EN

Stack Overflow用户
提问于 2020-09-18 16:02:20
回答 1查看 3.9K关注 0票数 1

试图从此脚本中提取图像填充量。

代码语言:javascript
复制
public class ProgressBar : MonoBehaviour
{
    public static int minimum;
    public static int maximum;
    public static int current;
    public Image mask;


    void Update()
    {
        GetCurrentFill();
    }

    void GetCurrentFill()
    {
        float currentOffset = current - minimum;
        float maximumOffset = maximum - minimum;
        float fillAmount = currentOffset / maximumOffset;
        mask.fillAmount = fillAmount;
    }
}

我将解释这段代码:

当前=现值,最小=最低经验到水平,最大=最大经验到水平

代码语言:javascript
复制
   if(skortotal < 20)
        {
            playerlevel = 1;
            ProgressBar.minimum = 0;
            ProgressBar.current = skortotal;
            ProgressBar.maximum = 20;
        }
        if(skortotal >= 20)
        {
            playerlevel = 2;
            ProgressBar.current = skortotal;
            ProgressBar.maximum = 50;
            ProgressBar.minimum = 20;
        }
}

代码已经起作用了,但我不知道如何使用lerp使其工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-18 16:53:57

您询问了如何使用Mathf.Lerp实现这一目标,下面是我的建议:

代码语言:javascript
复制
mask.fillAmount = Mathf.Lerp(minimum, maximum, current) / maximum;

Lerp将自动夹紧这些值,因此结果总是在0..1内。

要让动画化,填充随时间的推移,您可以尝试以下操作:

代码语言:javascript
复制
float actualValue = 0f; // the goal
float startValue = 0f; // animation start value
float displayValue = 0f; // value during animation
float timer = 0f;

// animate the value from startValue to actualValue using displayValue over time using timer. (needs to be called every frame in Update())
timer += Time.deltaTime;
displayValue = Mathf.Lerp(startValue, actualValue, timer);
mask.fillAmount = displayValue;

若要启动动画,请在更改actualValue时执行以下操作:

代码语言:javascript
复制
actualValue = * some new value *;
startValue = maskFillAmount; // remember amount at animation start
timer = 0f; // reset timer.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63959125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档