【问题标题】:Unity 3d reverse float valuesunity 3d 反向浮点值
【发布时间】:2020-05-11 23:05:08
【问题描述】:

我的迷你项目有两个按钮。 A 和 B。当我按下 A 时,值开始增加,当我按下 B 时,值开始减小。我的值从 190 开始,最大值为 200。我还有一个剩余值浮动。我想要的是当我按下 B 按钮并且我的值开始减少时,例如我的值达到 120。现在剩余的浮点数将显示差异,即 80(最大值 - 当前值)。此剩余值显示为 UI.Text。例如,当我按下 A 按钮时,剩余值会接近最大值。 79,78,77,76等。但我想要的是剩余值应该显示在UI.text上从0到80而不是80到0。希望你理解。请帮我解决这个问题。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class numvalues: MonoBehaviour {



    public float current_num = 190f;
    public Text current_numtxt;
    public float max_num = 200f;
    public float remaining_num;
    public Text remaining_numtxt;

    void Start () {

    }

    void Update()
    {

        if (Input.GetKeyDown(KeyCode.W))

            current_num -= 10 * Time.deltaTime;



        if (Input.GetKeyDown(KeyCode.X))

            current_num += 10 * Time.deltaTime;
            current_numtxt.text = current_num.ToString ("0");

            remaining_num = max_num - current_num; 
            remaining_numtxt.text = remaining_num.ToString ("0");

    }
}

【问题讨论】:

    标签: unity3d 3d numbers reverse timedelta


    【解决方案1】:

    尝试保留另一个变量 lastSpeed。当您设置模式时,将 lastSpeed 设置为您所处的速度(在这种情况下为 120),并且当您增加速度时,只需执行 currentValue - lastSpeed。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 2022-10-06
      相关资源
      最近更新 更多