首页 > 解决方案 > 为什么我的 TMP 的面部颜色只在 50% 和 150% 时更新?

问题描述

Ello,我目前正在为本地多人 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;
    }
}

Text Tracker / Color Changer(使用设置 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#unity3d

解决方案


是的,我的问题是 Math.Round 函数。它被设置为 0,所以它只会四舍五入到整数。增加它可以解决所有问题。


推荐阅读