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

UE5 C++ 创建3DWidgete 血条 再造成伤害

一.创建

二.UI里声明变量

创建类

public:UPROPERTY(EditAnywhere,BlueprintReadWrite,Category  = "MyWidget")float CurrentHealth = 100.0f;UPROPERTY(EditAnywhere,BlueprintReadWrite,Category = "MyWidget")float MaxHealth = 100.0f; //可以给默认值

绑定监听函数

函数里使用父类变量进行计算 百分比

三.在Character里声明

重写受伤虚函数 

#include "MyHealthWidget.h"
	UWidgetComponent* MyWidgetHealth;
	virtual float TakeDamage(float DamageAmount,FDamageEvent const &DamageEvent,class AController*EventInstigator,AActor* DamagerCausser)override; //重写系统内部的TakeDamage 

四.编写逻辑

创建对象

MyWidgetHealth = CreateDefaultSubobject<UWidgetComponent>(TEXT("MyWidgetComponent"));
MyWidgetHealth->SetupAttachment(RootComponent);
static ConstructorHelpers::FClassFinder<UUserWidget>WidgetClass(TEXT("/Script/UMGEditor.WidgetBlueprint'/Game/UMG_Health.UMG_Health_C'"));    //静态加载
MyWidgetHealth->SetWidgetClass(WidgetClass.Class);
MyWidgetHealth->SetRelativeLocation(FVector(0,0,100)); 
MyWidgetHealth->SetWidgetSpace(EWidgetSpace::Screen);  //  输出到屏幕
MyWidgetHealth->SetDrawSize(FVector2D(400,20));

受伤-5血量

float AMyCharacter::TakeDamage(float DamageAmount, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamagerCausser)
{UMyHealthWidget* MyWidget = Cast<UMyHealthWidget>(MyWidgetHealth->GetUserWidgetObject());if (MyWidget){if (MyWidget->CurrentHealth > 0){MyWidget->CurrentHealth -= 5.f;}else{return 0.f;}}return 0.0f;
}

五.造成角色伤害物体

#include "Kismet/GameplayStatics.h"  //Aplaydamage 函数库里的头文件
#include "MyCharacter.h"  //后面用于转化
UFUNCTION()
void BeginOverlapFunction(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void EndOverlapFunction( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex); //四个参数
// FComponentEndOverlapSignature, UPrimitiveComponent, OnComponentEndOverlap, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex
UFUNCTION()
void HitOverlaoFunction(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);
//UPrimitiveComponent*, HitComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, FVector, NormalImpulse, const FHitResult&, Hit

逻辑碰撞,判断是否是角色。再造成ApplyDamage伤害。


void AMyActor::BeginOverlapFunction(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{AMyCharacter* MyCharacter = Cast<AMyCharacter>(OtherActor); //转换if (MyCharacter){UGameplayStatics::ApplyDamage(MyCharacter,5.0f,nullptr,this,UDamageType::StaticClass());  //应用伤害}GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,TEXT("BeginOverLapEvent is Success"));MyParticle->Activate(); //激活
}void AMyActor::EndOverlapFunction( UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{MyParticle->Deactivate(); //失效GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("EndOverlapEvent is Success"));
}void AMyActor::HitOverlaoFunction(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("HitOverlapEvent is Success"));
}

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

相关文章:

  • Android 14 vold 分析(1)启动
  • 【云计算】混合云组成、应用场景、风险挑战
  • spring bean的继承和依赖
  • Swift中的字符串
  • MySQL基础-----约束详解
  • 【Unity】游戏场景添加后处理特效PostProcessing
  • STM32芯片软复位导致SRAM2的值被擦除话题
  • 【C++航海王:追寻罗杰的编程之路】异常——错误处理方式之一
  • 5.2 mybatis之autoMappingBehavior作用
  • 【算法一则】做算法学数据结构 - 简化路径 - 【栈】
  • OpenHarmony实战开发-如何使用Web预渲染实现功能介绍。
  • 三七互娱,oppo,快手25届暑期实习内推
  • InnoDB架构:内存篇
  • 8个Python高效数据分析的技巧
  • 暴力破解密码自动阻断
  • 【华为】Telnet实验配置
  • SAM功能改进VRP-SAM论文解读VRP-SAM: SAM with Visual Reference Prompt
  • MySQL truncate table 与 delete 清空表的区别和坑
  • Spring GA、PRE、SNAPSHOT 版本含义及区别
  • 一文看懂标准版和Pro版的区别
  • 腾讯云服务器价格表(腾讯云服务器报价表)
  • 试试把GPT和Suno结合起来用(附免费GPT)
  • SpringBoot修改菜品模块开发
  • Rust开发笔记 | 系统编程的守护神
  • dcoker+nginx解决前端本地开发跨域
  • 基于云开发和微信小程序的爱宠家系统
  • 光场相机建模与畸变校正改进方法
  • 面试算法-173-二叉树的直径
  • Python Typing模块
  • 【鸿蒙开发】饿了么页面练习