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"));
}
相关推荐
undefined&&懒洋洋1 小时前
Web和UE5像素流送、通信教程
前端·ue5
kissSimple6 小时前
UE行为树编辑器图文笔记
笔记·ue5·编辑器·unreal engine·unreal engine 5
draracle1 天前
如何将 cryptopp库移植到UE5内
ue5
龙智DevSecOps解决方案2 天前
Perforce演讲回顾(上):从UE项目Project Titan,看Helix Core在大型游戏开发中的版本控制与集成使用策略
游戏·ue5·源代码管理·perforce·helix core
我救我自己3 天前
UE5.4.3 录屏回放系统ReplaySystem蓝图版
java·前端·ue5
cainiao0806054 天前
UE4/UE5开发资源
ue5·ue4
charon87786 天前
Unreal 实现建造游戏|地面交互shader
游戏·ue5·游戏引擎·虚幻
周周的Unity小屋6 天前
UE5蓝图实战:动态墙上挖坑与自定义坑尺寸
ue5·蓝图挖坑
@Unity打怪升级8 天前
Unity 与虚幻引擎对比:两大游戏开发引擎的优劣分析
游戏·unity·ue5·游戏引擎·ue4·游戏程序·虚幻
timidcatt9 天前
独立游戏《Project:Survival》UE5C++开发日志0——游戏介绍
c++·游戏·ue5