当前位置:网站首页>[Unity entry plan] Use the double blood bar method to control the blood loss speed of the damage area
[Unity entry plan] Use the double blood bar method to control the blood loss speed of the damage area
2022-08-08 15:25:00 【flashinggg】
Table of Contents
1 damage reduction area too fast
Speed controls the speed of injury reduction
Tutorials Learned
1 damage reduction area too fast
As before, this blog is still based on the introductory tutorial of station B.The reason is also very simple. I encountered the following problems when I followed the tutorial to learn to set the blood drop area:
If you only use OnTriggerStay for damage reduction, since this call isframe, which will cause the problem of damage reduction area too fast.
Following the tutorial method, I modified the Sleeping Mode in the Rigidbody2D component of the player object, but the effect of delaying was not achieved, and there was no way to control the speed of blood reduction.
2 double health bar method
The so-called double health bar is actually adding two true and false blood reduction variables.
- The true health bar is used to reduce damage to the current blood volume
- The fake health bar is used to control damage reduction over time
Based on the original code given by the tutorial, I have newly defined some parameters:
- damageSpeed——public, users can control the speed of blood loss
- TruedamageHealth - Multiply with Speed to become true health bar reduction
- True&round - used to determine whether to call ChangeHealth()
For details, see the following trigger script for the damage reduction area
using System.Collections;using System.Collections.Generic;using UnityEngine;public class DamageZone : MonoBehaviour{public int damageHealth = -1;public int damageSpeed;private double TruedamageHealth = -0.02;private double True = 0;int round=1;private void OnTriggerStay2D(Collider2D other){RubyController rubyController=other.GetComponent();if(rubyController != null){True += damageSpeed*TruedamageHealth;if (round==1||True < round * damageHealth){round++;rubyController.ChangeHealth(damageHealth);}}}}
The ChangeHealth() method is a method defined in the script attached to the player object:
//Change the life value//amount is the operation of adding/reducing blood in the gamepublic void ChangeHealth(int amount){//Limit the current health value range to [0,maxHealth]currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);// outputif(currentHealth < maxHealth)Debug.Log("The current health value is: " + currentHealth + "/" + maxHealth);}
Among them, the case of round==1 is raised separately as one of the or conditions, because the situation of the game itself is considered.When the player walks into the Trigger, the blood will be reduced immediately, and Stay will slowly reduce the blood in the damage reduction range:
Enter the damage reduction trigger range -> immediately reduce damage -> stay in the damage reduction trigger range -> continue to dropblood
Speed controls damage reduction speed
Final effect
The player entered the damage zone
Injuries
Of course, the above is just a super simple method. In the future study, for example, the GUI blood bar reflects the blood volume, etc., and will be supplemented at that time.
边栏推荐
- 光弘科技:公司在印度为小米、三星、OPPO、诺基亚提供智能手机等产品的制造服务
- [Online interviewer] How to achieve deduplication and idempotency
- 领域驱动设计系列贫血模型和充血模型
- 连这几个网站都不知道,怪不得你的消息比别人落后
- 幂等性~~
- 在中国银河证券开户安全吗 齐齐哈尔股票开户
- 瑞吉外卖学习笔记2
- 返回分页查询分类并统计多对多关系表中各分类下的应用数量
- Zhaoqi Technology Innovation and Entrepreneurship Event Event Platform, Investment and Financing Matchmaking, Online Live Roadshow
- JS-BOM-通过或运算-可以实现缺省值
猜你喜欢
全志V853芯片Tina下RTSP环境搭建方法
JS-Bom-while(计算闰年)
sqllabs 1~6通关详解
JS - BOM - - can be achieved through calculation or default values
腾讯超大 Apache Pulsar 集群的客户端性能调优实践
本机Redis Desktop Manager连不上vmware的redis
Notes on the development of kindergarten enrollment registration system based on WeChat applet
从洞察到决策,一文解读标签画像体系建设方法论丨DTVision分析洞察篇
sqoop连接MySQL跟本机不一致是为什么
Install Update(Patches) on ESXi
随机推荐
SAP系统为什么要迁移上云?
在中国银河证券开户安全吗 齐齐哈尔股票开户
一文搞懂│XSS攻击、SQL注入、CSRF攻击、DDOS攻击、DNS劫持
18、学习MySQL ALTER命令
HMS Core Analysis Service Intelligent Operation Version 6.5.1 Launched
分布式架构服务调用
JS加法器(DOM)
小实验:实现一个用于计算(包括加减乘除)的小程序
JS-Bom-while(计算闰年)
JS-BOM-factorial calculation
“科林明伦杯”哈尔滨理工大学暑假训练赛 B吃雪糕 (异或思维题)(补题)
深度学习-神经网络原理1
P8352-[SDOI/SXOI2022]小N的独立集【dp套dp】
Redis RDB分析系统
如何制作网页
(1)通过FlinkSQL将数据写入mysql demo
UOJ#748-[UNR #6]机器人表演【dp】
让您知道华为云服务器的强大【华为云至简致远】
leetcode/number of palindromic substrings
leetcode/回文子字符串的个数