当前位置:网站首页>Unity 伤害值的显示
Unity 伤害值的显示
2022-06-11 04:11:00 【海 月】
需求
当玩家攻击角色时,将伤害的数值随机生成于小怪的画布上
数值由小变大,由透明变不透明
显示一段时间之后,数值逐渐褪色
实现方法
将显示伤害数值的文本作为预制体,并在上面挂载脚本

攻击判断成功时,克隆该预制体到小怪的画布上
思路
使用 DOLocalMove 对文本进行一段平移插值
使用 DoScale 对文本大小进行缩放插值
使用 DOFade 对文本透明度进行插值
使用 协程 暂停一段时间,再对文本透明度进行插值
代码
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class HurtValueAnim : MonoBehaviour
{
RectTransform rectTransform;
public Vector2 delta;
public float timeToShow;
public float waitTime;
public float timeToFade;
public float scale;
void Start()
{
rectTransform = GetComponent<RectTransform>();
rectTransform.DOLocalMove(new Vector3(rectTransform.anchoredPosition.x + delta.x, rectTransform.anchoredPosition.y + delta.y, 0), timeToShow);
rectTransform.DOScale(new Vector3(scale, scale, scale), timeToShow);
GetComponent<Text>().DOFade(1, timeToShow);
StartCoroutine("WaitForASecond", waitTime);
}
IEnumerator WaitForASecond(float waitTime)
{
Timer timer = new Timer("Fade");
timer.StartRecording();
while (true)
{
//当前帧执行到此处挂起,下一帧从此处恢复
yield return null;
if (timer.GetCurrentRecord() >= waitTime)
{
GetComponent<Text>().DOFade(0, timeToFade);
}
//终止
if (timer.GetCurrentRecord() >= waitTime + timeToFade)
{
timer.Reset();
StopCoroutine("WaitForASecond");
}
}
}
}当玩家被攻击时,调用小怪身上的如下函数
void PopUpHurtValue(int value)
{
GameObject hurtValue = Instantiate(Resources.Load<GameObject>("Prefabs/UI/Attack/HurtValue_Hide"), canvas);
hurtValue.GetComponent<Text>().text = value.ToString();
hurtValue.transform.localPosition = new Vector3(Random.Range(-800,800), Random.Range(-500, 500),0);
Destroy(hurtValue,5f);
}效果

边栏推荐
- JVM(2):内存结构、类的加载过程
- 使用工具类按一定规则读取Excel文件
- 数据类型的转换和条件控制语句
- Vulkan official example interpretation raytracingshadows & use the model here (1)
- Safe and borderless, Guanghe tongdai 5g module +ai intelligent security solution shines at CPSE Expo
- Introduction to the development and production functions of shop facade transfer and rental applet
- 从初代播种到落地生花,5G商用三周年“催生万物”
- Market prospect analysis and Research Report of single photon counting detector in 2022
- 数据分析师必知必会的统计学知识
- clickjacking漏洞的挖掘与利用
猜你喜欢

Guanghetong LTE CAT6 module fm101-cg, which supports CBRS band, took the lead in obtaining FCC certification

Feature selection algorithm based on bare bones particleswarm optimization

Introduction to the development and production functions of shop facade transfer and rental applet

2022 年 5 月产品大事记

Analysis of zero time technology | discover lightning loan attack

再聊数据中心网络

JVM (1): introduction, structure, operation and lifecycle

2022 love analysis · privacy computing vendor panoramic report | love Analysis Report

Vulkan official example interpretation raytracing

Watson K's Secret Diary
随机推荐
Introduction to the development and production functions of shop facade transfer and rental applet
【CustomView】Glide+BitmapTransformation 图片上下边框波浪处理(WaveTransformation)
JVM (1): introduction, structure, operation and lifecycle
Market prospect analysis and Research Report of single photon counting detector in 2022
零时科技 | Discover 闪电贷攻击事件分析
给你一个项目,你将如何开展性能测试工作?
How to invest in programming knowledge and reduce the impact of knowledge failure
从初代播种到落地生花,5G商用三周年“催生万物”
MySQL lock summary
A. Prefix range (C language)
项目架构演进
Pictures that make people feel calm and warm
Protection and bypass of repeated contracting
再聊数据中心网络
Sslstrip Ultimate - location hijacking
Red team shooting range with three-layer protection
Safe and borderless, Guanghe tongdai 5g module +ai intelligent security solution shines at CPSE Expo
Market prospect analysis and Research Report of nitrogen liquefier in 2022
Possible problems with password retrieval function (supplementary)
Guanghetong won the "science and Technology Collaboration Award" of Hello travel, driving two rounds of green industries to embrace digital intelligence transformation