当前位置: 首页 > article >正文

简单血条于小怪攻击模板

创建一个2d正方形(9-Sliced)命名为Player,在Player下面新建一个画布(Canvas)命名为PlayerHealthUI,在画布下面新建一个滑动条(Slider)命名为HealthBar

把PlayerHealthUI脚本挂载到PlayerHealthUI上,拖拽情况如图所示

using UnityEngine;
using UnityEngine.UI;public class PlayerHealthUI : MonoBehaviour
{[SerializeField] private Slider healthBar;[SerializeField] private Health health;//更新血条方法(注意要写成公有的)public void UpdateUI(){healthBar.value = health.Value;}//进入游戏的时候开启private void Awake(){//起始值:血条UI的起始值=health的初始值healthBar.maxValue = health.Value;//更新:血条UI的现在值=health的初始值healthBar.value = health.Value;}
}

把playerMovement脚本,Health脚本,Damageable脚本,挂载到Player对象上,拖拽情况如图所示

using UnityEngine;
using UnityEngine.InputSystem;
using DG.Tweening;public class playerMovement : MonoBehaviour
{[SerializeField]private Rigidbody2D rb;private Vector2 _inputDirection;[SerializeField]private float _speed;public void Move(InputAction.CallbackContext context){_inputDirection = context.ReadValue<Vector2>();}private void FixedUpdate(){var position = (Vector2)transform.position;var targetPosition = position + _inputDirection;if (position == targetPosition) return;rb.DOMove(targetPosition, _speed).SetSpeedBased();}
}

using UnityEngine;
using UnityEngine.Events;public class Health : MonoBehaviour
{[SerializeField] private int health;[SerializeField] private UnityEvent<int> healthchange;public int Value{get { return health; }}//减少血量public void DreaseHealth(int amount){//血量减去被传进来的值health -= amount;healthchange.Invoke(health);}}

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Damageable : MonoBehaviour
{[SerializeField]private Health health;//想要实现被伤害时变红闪烁一下[SerializeField]private SpriteRenderer spriteRenderer;public void TakeDamage(int damage){health.DreaseHealth(damage);//在DG.Tweening包中,记得引入//DOColor()需要的两个arguement:第一个是确定颜色,第二个是完成变换所需要的时间//再变回去需要SetLoops(),两个arguemengt,一个是变换几圈,一个是以什么方式变//红色到白色是一圈,红色到白色是另一圈//Yoyo:慢慢变换//还需要用ChangeStartValue()给一个起始颜色spriteRenderer.DOColor(Color.red,0.2f).SetLoops(2,LoopType.Yoyo).ChangeStartValue(Color.white);}
}

把Attack脚本和EnemyMovement脚本,挂载到Enemy对象上

using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class EnemyMovement : MonoBehaviour
{[SerializeField] private Rigidbody2D rb;[SerializeField] private float _speed;[SerializeField]private PlayerManager playerManager;private void FixedUpdate(){//玩家当前位置var playPosition = playerManager.position;//敌人自己当前位置坐标var position=(Vector2)transform.position;//当前距离距玩家位置(移动的方向)var direction=playPosition-position; //数字规范(方便计算)direction.Normalize();//目的坐标var targetPosition = position + direction;//向目的地移动rb.DOMove(targetPosition, _speed).SetSpeedBased();}}

using UnityEngine;
using Timers;public class Attack : MonoBehaviour
{private bool _canAttack = true;private void OnTriggerEnter2D(Collider2D col){DealDamage(col);}private void OnTriggerStay2D(Collider2D other){DealDamage(other);}private void CanAttack(){_canAttack=true;}private void DealDamage(Collider2D other){if (!_canAttack) return;//Player为标签if (other.CompareTag("Player")){var damageable = other.GetComponent<Damageable>();damageable.TakeDamage(10);//三个argumentTimersManager.SetTimer(this, 1, CanAttack);_canAttack = false;}}
}
PlayerManager脚本挂载到空物体Manager上面
using UnityEngine;//获取玩家位置
public class PlayerManager:MonoBehaviour
{[SerializeField] private Transform playerTransform;//任何脚本呼叫PlayerManager时,都可以获得玩家位置public Vector2 position{get { return playerTransform.position; }}
}

http://www.lryc.cn/news/2386055.html

相关文章:

  • Win11 系统登入时绑定微软邮箱导致用户名欠缺
  • 代码随想录算法训练营第四十六四十七天
  • 华硕FL8000U加装16G+32G=48G内存条
  • 前后端联调实战指南:Axios拦截器、CORS与JWT身份验证全解析
  • java高级 -Junit单元测试
  • 在 UVM验证环境中,验证 Out-of-Order或 Interleaving机制
  • V9数据库替换授权
  • 勇闯Chromium—— Chromium的多进程架构
  • Go语言中常量的命名规则详解
  • 软件质量保证与测试实验
  • 历年华东师范大学保研上机真题
  • 【C++】什么是静态库?什么是动态库?
  • 项目阅读:Instruction Defense
  • springboot中拦截器配置使用
  • 用 Python 构建自动驾驶的实时通信系统:让车辆“交流”起来!
  • 在机器学习中,L2正则化为什么能够缓过拟合?为何正则化等机制能够使一个“过度拟合训练集”的模型展现出更优的泛化性能?正则化
  • day36 python神经网络训练
  • k8s部署ELK补充篇:kubernetes-event-exporter收集Kubernetes集群中的事件
  • 【Excel VBA 】窗体控件分类
  • C++性能相关的部分内容
  • Spring Boot 项目中常用的 ORM 框架 (JPA/Hibernate) 在性能方面有哪些需要注意的点?
  • 基于大模型的大肠癌全流程预测与诊疗方案研究报告
  • 解决DeepSeek部署难题:提升效率与稳定性的关键策略
  • AI进行提问、改写、生图、联网搜索资料,嘎嘎方便!
  • GStreamer开发笔记(四):ubuntu搭建GStreamer基础开发环境以及基础Demo
  • 2021年认证杯SPSSPRO杯数学建模A题(第二阶段)医学图像的配准全过程文档及程序
  • CV中常用Backbone-3:Clip/SAM原理以及代码操作
  • RPC 协议详解、案例分析与应用场景
  • dify-plugin-daemon的.env配置文件
  • 【Python】开发工具uv