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,做相应的逻辑操作。

五.效果

相关推荐
星火撩猿1 天前
常见游戏引擎介绍与对比
unity·ue5·游戏引擎·godot
清流君1 天前
【MySQL】数据库 Navicat 可视化工具与 MySQL 命令行基本操作
数据库·人工智能·笔记·mysql·ue5·数字孪生
Involuter2 天前
UE5 Assimp 自用
ue5
电子云与长程纠缠2 天前
Unreal Niagara制作SubUV贴图翻页动画
学习·ue5·编辑器·贴图·niagara
子燕若水2 天前
“Daz to Unreal”将 G8 角色(包括表情)从 daz3d 导入到 UE5。在 UE5 中,我发现使用某个表情并与闭眼混合后,上眼睑出现了问题
3d·ue5
半天法师2 天前
UE5.2+VarjoXR3,Lumen、GI、Nanite无效的两种解决方案
ue5·xr·vr
ue星空2 天前
UE5摄像机画面没有填充满屏幕有黑边
ue5
李詹3 天前
游戏开发核心技术解析——从引擎架构到攻防体系的完整技能树
架构·ue5·游戏引擎·游戏程序·3dsmax·虚幻
子燕若水3 天前
UE5的 Modify Curve 蓝图节点
ue5
人宅5 天前
UE5有些场景的导航生成失败解决方法
ue5