当前位置:网站首页>[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.
边栏推荐
- Zhaoqi Technology Innovation and Entrepreneurship Event Event Platform, Investment and Financing Matchmaking, Online Live Roadshow
- Chat with wine and chat, build an asynchronous non-blocking (aioredis) real-time (websocket) communication chat system based on Vue3.0+Tornado6.1+Redis publish-subscribe (pubsub) mode
- 什么是低代码开发?大家都真的看好低代码开发吗?
- JS-BOM-Name Converter - Input Name Position Reversed
- Redis RDB分析系统
- 解决Redis、MySQL缓存双写不一致问题
- 并发请求如何优雅地处理重复请求
- A16z:为什么 NFT 创作者要选择 cc0?
- JS-BOM-for,if(字符串转大小写)
- JS-BOM-通过或运算-可以实现缺省值
猜你喜欢
2022年8月中国数据库排行榜:openGauss重夺榜眼,PolarDB反超人大金仓
ThinkPHP3.2链接带中文参数乱码导致分页数据错误
从洞察到决策,一文解读标签画像体系建设方法论丨DTVision分析洞察篇
Thoroughly understand the volatile keyword and application scenarios, and it is a must for interviews, and Xiaobai can understand it!
A16z:为什么 NFT 创作者要选择 cc0?
JS-BOM-for, if (string to case)
文献阅读_VistaNet:用于多模式情绪分析的视觉方面注意网络
【Unity入门计划】制作RubyAdventure02-处理瓦片地图&碰撞
1
跟我一起来学弹性云服务器ECS【华为云至简致远】
随机推荐
幂等性~~
1052. The Angry Bookstore Boss
兆骑科创创业赛事活动举办平台,投融资对接,线上直播路演
1052. 爱生气的书店老板
JS-BOM-factorial calculation
sqoop连接MySQL跟本机不一致是为什么
跟我一起来学弹性云服务器ECS【华为云至简致远】
连这几个网站都不知道,怪不得你的消息比别人落后
bzoj3693 圆桌会议 hall定理+线段树
JS-BOM-for,if(字符串转大小写)
领域驱动设计系列贫血模型和充血模型
Tungsten Fabric SDN — OpenStack 与 Kubernetes 异构集群统一 SDN 方案
Interview questions 17.05. Letters and numbers
JS加法器(DOM)
想要精准营销,从学习搭建一套对的标签体系开始丨 DTVision 分析洞察篇
JS-BOM-阶乘计算
【愚公系列】华为云云数据库MySQL的体验流程|【华为云至简致远】
Kubernetes-基础-常用命令
什么是低代码开发?大家都真的看好低代码开发吗?
CS231n: 6 training neural network (2)