UE5 C++ 3D血条 响应人物受伤 案例

一.3Dwidget

1.创建C++ Userwidget的 MyHealthWidget,声明当前血量和最大血量

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

};

2.创建UMG的蓝图,ClassSetting里 继承 MyHealthWidget.

二.渲染到屏幕

在Charactor的构造函数中,构建子组件。并且静态加载UMG类。设置位置,屏幕,大小等。

cpp 复制代码
	UWidgetComponent* MyWidgetHealth;
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));

三.碰撞伤害

1.在Actor 添加头文件 和 碰撞函数

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

2.在CPP里实现 ,如果人物刚碰撞 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(); //激活
}

四.人物收到伤害 血条响应

重写内部受伤函数

cpp 复制代码
	virtual float TakeDamage(float DamageAmount,FDamageEvent const &DamageEvent,class AController*EventInstigator,AActor* DamagerCausser)override; //重写系统内部的TakeDamage 
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;
}

获得指针UserWidget 转换为 UMyHealthWidget,做相应的逻辑操作。

五.效果

相关推荐
mengzhi啊9 天前
UE5角色模型替换核心:设置领导者姿势组件
ue5
mengzhi啊13 天前
UE5 沙漠添加植物,然后在湖泊游泳。关键词:湖泊使用蓝图的重叠事件,湖泊的碰撞偏移。所有的物体都可以使用蓝图来使用它
ue5
Zhichao_9720 天前
【UE5.8】VS2026安装
ue5
DolitD20 天前
UE像素流与云渲染技术方案对比分析
3d·云原生·ue5·云计算·图形渲染
Дерек的学习记录22 天前
Unreal Engine 5:第三人称运动系统和人物动画
开发语言·学习·ue5
努力学习的小廉25 天前
UE5 入门系列(三):核心框架类全解析
ue5
平行云1 个月前
建筑BIM/3D可视化轻量级实时评审与分享方案
unity·ue5·3dsmax·实时云渲染·云桌面·云游戏·建筑bim
BugPhysician1 个月前
影之刃零首发UE崩溃排查:着色器缓存、虚拟内存与驱动版本
ue5·影之刃零·影之刃零报错·影之刃零进不去·ue崩溃·ue引擎崩溃·ue5引擎崩溃
电子云与长程纠缠1 个月前
UE5魂系游戏开发教程05
开发语言·学习·游戏·ui·ue5
fanfan_hongyun1 个月前
UE5.1 蓝图调用C++方法
ue5