Unity implements damage floating word

Although a 2D template is used, the important implementation part is basically the same for 2D and 3D.

In order to realize the harm floating word, you first need to create a text component. Here I use the TextMeshPro_Text(UI) component type. It is recommended to directly create a UI text of Text type under an empty object.

After setting the character format in the Text component according to your needs, (the color and size will vary according to personal usage habits and different scenarios, but basically the bytes need to be centered) just set it as a prefab .

Then how to generate the font on the head of the enemy? You only need to let the enemy create this text object at the position being attacked, and then place the scripts that control the various display effects of the text object in the text object.

Here is the code that controls the generation of the text object

//该脚本要挂载都敌人组件上,在敌人的血条改变或者受到伤害时调用该方法
    float rand = Random.Range(-2, 2);//随机-2~2的值

    Vector3 new_position = transform.position;//将受到攻击的敌人的位置转换为Vector3

    //在位置上生成文本对象
    new_position.x += rand;
    GameObject gameObject = Instantiate(Text, new_position, Quaternion.identity);

    //给文本对象中的文本赋值,显示数值
    gameObject.GetComponentInChildren<Hurt_Text>().Init(-change);

The function of the random value is to prevent the generated values ​​from being concentrated in one place, because sometimes your attack frequency is too fast, which may cause several bytes to overlap, which is very bad.

This is the code that controls the text object to run

public class Hurt_Text : MonoBehaviour
{
    private TMP_Text text;//字符文本组件

    float alpha;//阿尔法值,控制字体逐渐消失的效果

    [Range(0, 10)]
    public float speed;//飘动速度

    [Range(0, 1)]
    public float speed_weak;//速度衰弱

    [Range(0, 50)]
    public float dis_time;//消失时间

    [Range(0, 10)]
    public float weak_time;//开始衰弱时间

    public Color color;//文本颜色

    //这个必须提醒一下,必须使用Awake而Start不行,因为在上一段文本赋值时
    //到文本对象脚本的Start还没有执行到,会造成text为null异常
    void Awake()
    {
        alpha = 1;
        text = GetComponentInChildren<TMP_Text>();
        Destroy(gameObject, dis_time);
        text.color = color;
    }

    void Update()
    {
        transform.Translate(0, speed * Time.deltaTime, 0);//使字体向上移动
        if (speed > 0)
        {
            speed -= speed_weak;//使得向上移动的速度逐渐减少
        }
        else if (speed <= 0 && weak_time >0)
        {
            weak_time -= Time.deltaTime;//当速度为0后,字体颜色开始透明的时间
        }
        else if (alpha > 0 && weak_time <=0) 
        {
            alpha -= 0.01f;//控制字体逐渐透明
        }
        text.color = new Color(text.color.r, text.color.g, text.color.b, alpha);//修改字体颜色,透明度等
    }

    public void Init(int hurt)
    {
        text.text = hurt.ToString();//设置文本
    }
}

Of course, the effect I want to achieve is to float upwards for a certain distance and then stop, and then slowly disappear after a certain period of time. When achieving other effects, you can rewrite it according to your own understanding. Of course, there is actually a little redundant part here, because in alpha (Control the font color transparency, 1 is completely opaque, 0 is completely transparent) After reaching 0, the font is actually invisible. At this time, just destroy the component directly. There is no need to set the disappearance time. Of course, if you want to achieve other effects or You can keep it if you don't want it to fade away.

I have to make complaints about what I saw on CSDN to do unity damage floating characters. There is a part of the explanation that is too unfriendly to Mengxin (yes, Mengxin is talking about myself), lacking detailed instructions and code process explanations, and even some Without commenting on the code...

Let's take a look at the final realization effect of the tracking bullet in the previous issue

Guess you like

Origin blog.csdn.net/WEIWEI6661012/article/details/129630051