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

简单血条于小怪攻击模板

创建一个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.xdnf.cn/news/8733.html

相关文章:

  • 【Linux】深刻理解OS管理
  • Java 8 Stream操作示例
  • 《仿盒马》app开发技术分享-- 原生地图展示(端云一体)
  • 【Python】1. 基础语法(1)
  • webpack优化方法
  • 学习日记-day15-5.25
  • 云原生安全之HTTP协议:从基础到实战的安全指南
  • JavaSE核心知识点03高级特性03-05(注解)
  • 后经济时代开源链动2+1模式AI智能名片S2B2C商城小程序驱动零售企业转型研究
  • JAVA 项目中 maven pom.xml 和 properties 配置文件、spring 配置文件,以及环境变量的关系
  • 《STL--list的使用及其底层实现》
  • 基于aspnet,微信小程序,mysql数据库,在线微信小程序汽车故障预约系统
  • Win11 系统登入时绑定微软邮箱导致用户名欠缺
  • 【TDengine源码阅读】taosMemoryDbgInit函数
  • 基于MATLAB实现传统谱减法以及两种改进的谱减法(增益函数谱减法、多带谱减法)的语音增强
  • 全面理解类和对象(下)
  • 5.25本日总结
  • 计算机网络-WebSocket/DNS/Cookie/Session/Token/Jwt/Nginx
  • DAY36
  • GitLab-CI简介
  • 单链表的排序
  • Collection集合遍历的三种方法
  • multiprocessing多进程使用案例
  • 用神经网络对信贷项目进行预测
  • java三种常见设计模式,工厂、策略、责任链
  • 原生php单元测试
  • bun全栈开发尝鲜:用bun-react-template实现Markdown文章展示
  • removeIf() 方法,结合 Lambda 表达式
  • 鸿蒙仓颉开发语言实战教程:页面跳转和传参
  • WORD 转 PDF 工具:排版 / 图片 / 表格批量转换提升办公效率