반응형
- 코드 작성
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class HudDamageText : MonoBehaviour
{
public float moveSpeed = 1;
public float alphaSpeed = 5;
public float destroyTime = 2;
public TextMeshPro text;
Color alpha;
public string damage;
private void Start()
{
text = GetComponent<TextMeshPro>();
text.text = damage.ToString();
alpha = text.color;
Invoke("DestroyObject", destroyTime);
}
private void Update()
{
transform.Translate(new Vector3(0, moveSpeed * Time.deltaTime, 0));
alpha.a = Mathf.Lerp(alpha.a, 0, Time.deltaTime * alphaSpeed);
text.color = alpha;
}
private void DestroyObject()
{
Destroy(gameObject);
}
}
- 데미지를 입었을 때
public void TakeDamage(int damage, DamageType type)
{
GameObject hudText = Instantiate(hudDamageText);
hudText.GetComponent<HudDamageText>().damage = damage;
hudText.transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, -0.5f);
switch (type) //데미지 타입에 따라 글자 색깔을 변경하고 싶을때
{
case DamageType.One:
hudText.GetComponent<HudText>().damage = "<color=red>" + damage + "</color>";
break;
case DamageType.Two:
hudText.GetComponent<HudText>().damage = "<color=green>" + damage + "</color>";
break;
case DamageType.Three:
hudText.GetComponent<HudText>().damage = "<color=blue>" + damage + "</color>";
break;
}
}
반응형