Unity damage digital display
Reference link https://blog.csdn.net/book_longssl/article/details/39610775?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-7&spm=1001.2101.3001.4242
Try to make the display effect of the damage number as follows
The first is to create a new c# script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamageGUI : MonoBehaviour
{
private Vector3 mTarget;
private Vector3 mScreen;
public float value;
public float ContentWidth = 100;
public float ContentHeight = 50;
private Vector2 mPoint; //GUI坐标
public float DestoryTime = 2.0f; //伤害数字消失时间
// Start is called before the first frame update
void Start()
{
mTarget = transform.position; //获取目标位置
mScreen = Camera.main.WorldToScreenPoint(mTarget); //转化为屏幕位置
mPoint = new Vector2(mScreen.x, Screen.height - mScreen.y);
StartCoroutine("Free"); //开启一个协程
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.up * 0.5f * Time.deltaTime); //伤害数字上移效果
mTarget = transform.position;
mScreen = Camera.main.WorldToScreenPoint(mTarget);
mPoint = new Vector2(mScreen.x, Screen.height - mScreen.y);//实时变化位置
}
void OnGUI()
{
if (mScreen.z > 0) //伤害数字显示
{
GUI.Label(new Rect(mPoint.x, mPoint.y, ContentWidth, ContentHeight), value.ToString());
}
}
IEnumerator Free() //协程,伤害数字时间一到消失
{
yield return new WaitForSeconds(DestoryTime);
Destroy(this.gameObject);
}
}
Create an empty object
at will, name it DamageText, insert the previously written DamageGUI script
and set it as a prefab, and put it into the Resources folder
Then
I created a new statusBar at the top of the character’s head. I created a new statusBar. It can also be used to make vertigo, slowdown, etc.
Then call this DamageText prefab in the previously written combat module.
public GameObject DamageGUI;
void Start()
{
DamageGUI = Resources.Load("DamageText") as GameObject;
}
public void AddHp(float value) {
GameObject DamageText = (GameObject)Instantiate(DamageGUI, transform.DeepFind("statusBar").gameObject.transform.position, Quaternion.identity);
DamageText.GetComponent<DamageGUI>().value = value;
}
The script that calls this prefab is located in the PlayerHandle layer
The initial Damage GUI is empty and the prefab needs to be dragged in, but in order not to drag in every time a new character monster is created, here I use
DamageGUI = Resources.Load("DamageText") as GameObject;
Get the prefabs you put in before from the Resources folder, and you can get them automatically after running
At the same time, the starting position to generate the damage number is also to search downwards
transform.DeepFind("statusBar").gameObject.transform.position
The code of DeepFind is as follows
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class TransformHelpers
{
public static Transform DeepFind(this Transform parent, string targetName) {
Transform tempTrans = null;
foreach (Transform child in parent)
{
if (child.name == targetName)
{
return child;
}
else {
tempTrans = DeepFind(child, targetName);
if (tempTrans != null) {
return tempTrans;
}
}
}
return tempTrans;
}
}
The simple white-printed damage number is completed. After that, you need to add styles. For example, you need to change the size of the damage number. It should be the largest when you jump out, and then gradually become smaller. At the same time, if it is a crit, you have to change the color, and it will gradually become transparent over time, etc. , To be completed later.