跳到主要内容

0 - 常用小技巧

本文记录一些UE5开发中的常用小技巧,随时间补充。

Actor

Actor伤害

UE5中对Actor受到的伤害内置了3种类型:

  • 任意伤害(Any Damage)
  • 点伤害(Point Damage)
  • 范围伤害(Radial Damage)

要想让Actor受到伤害,得用UGameplayStatics::ApplyDamage系列函数:

// #include "Kismet/GameplayStatics.h"

static float UGameplayStatics::ApplyDamage(
AActor* DamagedActor,
float BaseDamage,
AController* EventInstigator,
AActor* DamageCauser,
TSubclassOf<class UDamageType> DamageTypeClass);

static float ApplyPointDamage(
AActor* DamagedActor,
float BaseDamage,
const FVector& HitFromDirection,
const FHitResult& HitInfo,
AController* EventInstigator,
AActor* DamageCauser,
TSubclassOf<class UDamageType> DamageTypeClass);

static bool ApplyRadialDamage(
const UObject* WorldContextObject,
float BaseDamage,
const FVector& Origin,
float DamageRadius,
TSubclassOf<class UDamageType> DamageTypeClass,
const TArray<AActor*>& IgnoreActors,
AActor* DamageCauser = NULL,
AController* InstigatedByController = NULL,
bool bDoFullDamage = false,
ECollisionChannel DamagePreventionChannel = ECC_Visibility);

Actor受到伤害后,会执行AActor::TakeDamage系列函数,需要我们重写这些函数以实现自定义逻辑:

// 任意类型
virtual float AActor::TakeDamage(float DamageAmount, struct FDamageEvent const& DamageEvent, class AController* EventInstigator, AActor* DamageCauser);

// 点伤害类型
virtual float AActor::InternalTakePointDamage(float Damage, struct FPointDamageEvent const& PointDamageEvent, class AController* EventInstigator, AActor* DamageCauser);

// 范围伤害类型
virtual float AActor::InternalTakeRadialDamage(float Damage, struct FRadialDamageEvent const& RadialDamageEvent, class AController* EventInstigator, AActor* DamageCauser);

Actor所有权

有时会让Actor“拥有”其他Actor,可通过AActor::SetOwner()进行设定。如果该Actor的拥有者是Pawn类型的,可以通过AActor::SetInstigator()实现。

项目设置

设置路径备注
Generation/Cell Size可控制NavMesh孔洞的大小,让AI寻路更灵活
Generation/Cell Height可控制NavMesh的生成高度
Runtime/Runtime Generation是否在游戏运行时动态生成NavMesh

参考资料