一.3Dwidget
1.创建C++ Userwidget的 MyHealthWidget,声明当前血量和最大血量
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.
3.
二.渲染到屏幕
在Charactor的构造函数中,构建子组件。并且静态加载UMG类。设置位置,屏幕,大小等。
UWidgetComponent* MyWidgetHealth;
MyWidgetHealth = CreateDefaultSubobject(TEXT("MyWidgetComponent"));
MyWidgetHealth->SetupAttachment(RootComponent);
static ConstructorHelpers::FClassFinderWidgetClass(TEXT("/Script/UMGEditor.WidgetBlueprint'/Game/UMG_Health.UMG_Health_C'")); //静态加载
MyWidgetHealth->SetWidgetClass(WidgetClass.Class);
MyWidgetHealth->SetRelativ服务器托管网eLocation(FVector(0,0,100));
MyWidgetHealth->SetWidgetSpace(EWidgetSpace::Screen); // 输出到屏幕
MyWidgetHealth->SetDrawSize(FVector2D(400,20));
三服务器托管网.碰撞伤害
1.在Actor 添加头文件 和 碰撞函数
#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
2.在CPP里实现 ,如果人物刚碰撞 ApplyDamage();
void AMyActor::BeginOverlapFunction(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
AMyCharacter* MyCharacter = Cast(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(); //激活
}
四.人物收到伤害 血条响应
重写内部受伤函数
virtual float TakeDamage(float DamageAmount,FDamageEvent const &DamageEvent,class AController*EventInstigator,AActor* DamagerCausser)override; //重写系统内部的TakeDamage
float AMyCharacter::TakeDamage(float DamageAmount, FDamageEvent const& DamageEvent, AController* EventInstigator, AActor* DamagerCausser)
{
UMyHealthWidget* MyWidget = Cast(MyWidgetHealth->GetUserWidgetObject());
if (MyWidget)
{
if (MyWidget->CurrentHealth > 0)
{
MyWidget->CurrentHealth -= 5.f;
}
else
{
return 0.f;
}
}
return 0.0f;
}
获得指针UserWidget 转换为 UMyHealthWidget,做相应的逻辑操作。
五.效果
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
服务器托管网 大家好,我是小悟 支付宝小程序备案整体流程总共分为五个环节:备案信息填写、平台初审、工信部短信核验、通管局审核和备案成功。 服务商可以代小程序发起备案申请。在申请小程序备案之前,需要确保小程序基本信息已填写完成、小程序至少存在一个审核通过的类目。…