为什么我的 TMP 的脸色只在 50% 和 150% 时更新?

Why does my TMP's face color update only at 50% and 150%?

提问人:HarrisonO 提问时间:2/17/2021 更新时间:2/17/2021 访问量:44

问:

埃洛, 我目前正在为本地多人roguelike游戏开发一种机制,玩家之间的距离越近,他们造成的伤害就越大。到目前为止,我已经让乘数工作并更改伤害统计数据,但我无法像总乘数那样更新我的百分比文本。在当前状态下,文本网格专业人士的面部颜色仅以 50% 和 150% 的速度更新,而它应该每帧都更新一次。关于为什么会这样或任何潜在的修复方法的任何想法?

谢谢

统计(用于 totalMultiplier)

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

public class Stats : MonoBehaviour
{
    public GameObject midpoint;
    public float baseDamage;
    [SerializeField] private float damage;
    [SerializeField] private float multiplier = 1;
    public float totalMultiplier;
    private float distMulti;
    // Start is called before the first frame update
    void Start()
    {
        multiplier = 1;
    }

    // Update is called once per frame
    void Update()
    {
        //actual multiplier
        damage = (baseDamage * multiplier) / (midpoint.GetComponent<Midpoint>().distance * 0.5f);
        if (damage >= (baseDamage * multiplier * 2))
        {
            damage = (baseDamage * multiplier * 2);
        }
        //Keep track of multiplier
        totalMultiplier = damage / baseDamage;
    }
}

文本跟踪器/颜色转换器(使用设置 faceColor 编辑 TMP 着色器)

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

public class TextTracker : MonoBehaviour
{
    public GameObject player;
    private TextMeshPro text;
    private Color redAmount = Color.red;
    // Start is called before the first frame update
    void Start()
    {
        text = GetComponent<TextMeshPro>();

    }

    // Update is called once per frame
    void Update()
    {
        text.SetText(System.Math.Round((player.GetComponent<Stats>().totalMultiplier)* 100, 0).ToString() + "%");
        transform.position = new Vector2(player.transform.position.x, player.transform.position.y + 1);
        Shader color = text.material.shader;

        text.faceColor = new Color(1f, 1f - (float)System.Math.Round(player.GetComponent<Stats>().totalMultiplier) / 2, 1f - (float)System.Math.Round(player.GetComponent<Stats>().totalMultiplier) / 2, 1f);

    }
}
C# Unity-游戏引擎

评论

0赞 HarrisonO 2/17/2021
男孩们,我有点慢。刚刚意识到问题出在我的四舍五入上。只需增加小数点的数量即可。它卡在 0.5 和 1。感谢所有的帮助!

答:

0赞 HarrisonO 2/17/2021 #1

是的,我的问题是Math.Round函数。它被设置为 0,因此它只会四舍五入为整数。增加它修复了这一切。