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

一.创建

二.UI里声明变量

创建类

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

绑定监听函数

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

三.在Character里声明

重写受伤虚函数

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

四.编写逻辑

创建对象

cpp 复制代码
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血量

cpp 复制代码
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;
}

五.造成角色伤害物体

cpp 复制代码
#include "Kismet/GameplayStatics.h"  //Aplaydamage 函数库里的头文件
#include "MyCharacter.h"  //后面用于转化
cpp 复制代码
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伤害。

cpp 复制代码
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"));
}
相关推荐
zhangzhangkeji12 天前
UE5 C++(71):文件是否存在,文件夹是否存在,FPaths :: FileExists( const FString & InPath) ;
ue5
妙为12 天前
UE5角色穿过石头穿模
ue5·unreal engine5·角色穿越石头·穿模
技术策划Boring13 天前
2025年工作复盘:开放世界3A项目配置管线与性能监控的探索
游戏·ue5·虚幻·p4·perforce
zhangzhangkeji15 天前
UE5 C++(70-2):定义成员函数 getCleanDirectory(..) 和枚举类 EFileDirectoryType,来获得目录
ue5
avi911116 天前
UE4-UE5虚幻引擎-前置学习三,优化,基础CPP
ue5·ue4·游戏开发·虚幻·游戏优化·游戏代码
zhangzhangkeji16 天前
UE5线程进阶(3-2):任务图的相关源码整理。 FGraphEvent 与 TGraphTask 的区别和联系
ue5
zhangzhangkeji18 天前
UE5线程进阶(3-1):
ue5
zhangzhangkeji18 天前
UE5线程进阶(2-3):enum ENamedThreads命名空间 :: Type : int32 { RHIThread = 0 } 是渲染硬件接口线程
ue5
zhangzhangkeji19 天前
UE5线程进阶(2-1):枚举类EAsyncExecution,作业类TAsyncRunnable、TAsyncQueuedWork,及全局线程函数 Async(..),及线程调用的 4 种方法总结
ue5
zhangzhangkeji20 天前
UE5线程进阶(1):
ue5