当前位置:网站首页>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);
}效果

边栏推荐
- Market prospect analysis and Research Report of single photon counting detector in 2022
- Guanghetong 5g module shines brightly and has won the "2021 science and technology award of China Electronics Society"
- Implementation of one-dimensional convolutional neural network CNN based on FPGA (V) data quantization (with code)
- Final review of software engineering notes (short answer)
- MySQL锁总结
- AI助力,释放法务势能!iTerms合同智审系统重磅发布
- Fundamentals of embedded audio processing
- 如何检查域名解析是否生效?
- Grandpa simayan told you what is called inside roll!
- 数字电影的KDM是什么?
猜你喜欢

2022-06-10:薯队长从北向南穿过一片红薯地(南北长M,东西宽N),红薯地被划分为1x1的方格, 他可以从北边的任何一个格子出发,到达南边的任何一个格子, 但每一步只能走到东南、正南、西南方向的

项目架构演进

写给通信年轻人的27个忠告

Matter protocol

Statistical knowledge required by data analysts

Pictures that make people feel calm and warm
![[激光器原理与应用-2]:国内激光器重点品牌](/img/55/a87169bb75429f323159e3b8627cc6.jpg)
[激光器原理与应用-2]:国内激光器重点品牌

Eth relay interface

Guanghetong officially released the sc126 series of intelligent modules to promote more intelligent connection

JVM(1):介绍、结构、运行和生命周期
随机推荐
Vulkan official example interpretation raytracing
CES 2022 𞓜 guanghetong LTE module fm101 has obtained two important ce/fcc certifications with excellent performance
Radar emitter modulation signal simulation (code)
What is the time-consuming domain name resolution? What are the influencing factors of domain name resolution time?
为了实现零丢包,数据中心网络到底有多拼?
Watson K's Secret Diary
[network] socket programming
June 10, 2022: Captain Shu crosses a sweet potato field from north to South (m long from north to South and N wide from east to West). The sweet potato field is divided into 1x1 squares. He can start
Esp32 development -lvgl display picture
使用工具类按一定规则读取Excel文件
Esp32 development -lvgl animation display
Safe and borderless, Guanghe tongdai 5g module +ai intelligent security solution shines at CPSE Expo
Market prospect analysis and Research Report of Ethernet scanner in 2022
Fundamentals of embedded audio processing
Market prospect analysis and Research Report of pipe and hose press fitting tools in 2022
Esp32 development -lvgl uses internal and external fonts
未来已来,5G-Advanced时代开启
Zhongang Mining: fluorochemical industry is the main consumption field of fluorite
Vulkan-官方示例解读-Shadows(光栅化)
零时科技 | Discover 闪电贷攻击事件分析