Unity效果实现:扣血显示
在众多游戏中,敌人受到伤害后会弹出一个数值,显示收到了多少伤害,我们用Unity3D来实现这一效果
实现思路:
在敌人收到攻击后,生成一个文本控件,上面显示敌人受到的伤害
代码1(挂在文本控件上):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class HpControl : MonoBehaviour
{
private float timer = 0;
public void SetText(string text)
{
GetComponent<TMP_Text>().text = text;
}
// Start is called before the first frame update
// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
if(timer>1)
{
Destroy(gameObject);
}
transform.Translate(Vector3.up * Time.deltaTime);
}
}
代码2(挂在画布上,控制文本控件的显示与消失,和其中的内容):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HpManager : MonoBehaviour
{
//关联HpText预制件
public GameObject HpTextPre;
// Start is called before the first frame update
public void ShowText(string text)
{
GameObject go = Instantiate(HpTextPre, transform);
go.GetComponent<HpControl>().SetText(text);
}
// Update is called once per frame
void Update()
{
//面向摄像机
transform.rotation = Quaternion.LookRotation(Camera.main.transform.forward);
}
}

浙公网安备 33010602011771号