UE4【C++】子d伤害系统使用

UE4【C++】子d伤害系统使用,第1张

概述本文章向大家介绍UE4【C++】子d伤害系统使用,需要的朋友可以参考一下

可能很多初学者如果想知道UE4的这种伤害系统如何使用起来会有点迷茫。

我自己做了demo给大家参考下。

首先要有个子d类。

.h代码如下

1 UCLASS()

2 class SNVRCLAYSHOOTING_API AProjectile : public AActor

3 {

4 GENERATED_BODY()

5

6 public:

7 // Sets default values for this actor's propertIEs

8 AProjectile();

9

10 UPROPERTY(VisibleAnywhere,BlueprintReaDWrite,category = "Projectile")

11 UStaticmeshComponent* ProjectileMesh;

12

13 UPROPERTY(VisibleAnywhere,category = "Projectile")

14 UParticleSystemComponent* LaunchParticle; //烟火,开q时

15

16 UPROPERTY(VisibleAnywhere,category = "Projectile")

17 UProjectileMovementComponent *ProjectileMovementComponent = nullptr;

18

19 protected:

20 // Called when the game starts or when spawned

21 virtual voID BeginPlay() overrIDe;

22

23 UFUNCTION()

24 voID OnHit(UPrimitiveComponent* HitComponent,AActor* OtherActor,UPrimitiveComponent* OtherComp,FVector normalimpulse,const FHitResult& Hit);

25

26 public:

27 // Called every frame

28 virtual voID Tick(float DeltaTime) overrIDe;

29

30 UFUNCTION(BlueprintCallable,category = "Projectile_Fun")

31 voID LaunchProjectile(FVector Speed);

32

33 };

VIEw Code

.cpp代码如下

1 AProjectile::AProjectile()

2 {

3 // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.

4 PrimaryActorTick.bCanEverTick = true;

5

6 ProjectileMovementComponent = CreateDefaultSubobject(Fname("ProjectileMovement")); //生成组件

7 ProjectileMovementComponent->bautoActivate = false; //自动飞行调成false

8

9 ProjectileMesh = CreateDefaultSubobject(Fname("ProjectileMesh"));

10 RootComponent = ProjectileMesh;

11 ProjectileMesh->SetNotifyRigIDBodyCollision(true);

12 ProjectileMesh->SetVisibility(true);

13

14 LaunchParticle = CreateDefaultSubobject(Fname("LaunchParticle"));

15 LaunchParticle->SetupAttachment(ProjectileMesh); //将粒子效果绑定在根结点上(即ProjectileMesh)

16 LaunchParticle->SetautoActivate(true); //创建好就启动

17

18 ProjectileMesh->OnComponentHit.AddDynamic(this,&AProjectile::OnHit);

19

20 InitiallifeSpan = 5.0f;

21 }

22

23 // Called when the game starts or when spawned

24 voID AProjectile::BeginPlay()

25 {

26 Super::BeginPlay();

27

28 }

29

30 // Called every frame

31 voID AProjectile::Tick(float DeltaTime)

32 {

33 Super::Tick(DeltaTime);

34

35 }

36 voID AProjectile::LaunchProjectile(FVector Speed)

37 {

38 ProjectileMovementComponent->SetVeLocityInLocalSpace(Speed);

39 ProjectileMovementComponent->Activate(); //可以飞行了

40 }

41 //碰撞后发生的事件

42 voID AProjectile::OnHit(UPrimitiveComponent* HitComponent,const FHitResult& Hit)

43 {

44 UE_LOG(LogTemp,Warning,TEXT("AProjectile::OnHit--OtherActor.name=%s"),*OtherActor->Getname()); //撞击后给个提示

45

46 ProjectileMesh->SetNotifyRigIDBodyCollision(false); //碰撞之后再也不发生碰撞事件

47 //CollisionMesh->DestroyComponent(); //不是DestroyActor,Component是单一一个

48

49 //受伤害源OtherActor的Takedamage()会被调用

50 UGameplayStatics::ApplyPointdamage(OtherActor,10.0f,GetActorLocation(),Hit,NulL,UdamageType::Staticclass());

51

52 Destroy();

53 }

VIEw Code

伤害的入口从OnHit看出,调用了UGameplayStatics::ApplyPointdamage;

第一个参数就是受伤害源,UE4的伤害系统会调用受伤害源的Takedamege接口;

此时子d有了伤害力,那么受伤害源就要被伤害;

每个actor都有这个接口virtual float Takedamage(float damageAmount,struct FdamageEvent const& damageEvent,class AController* EventInstigator,AActor* damageCauser);

只要重写这个接口,写上自己的处理逻辑。

受伤害源部分代码如下:

1 float AdamagedActor::Takedamage(float damageAmount,AActor* damageCauser)

2 {

3 float actuallyApplIEd = 0.0f;

4

5 MaxHP -= damageAmount;

6

7 UE_LOG(LogTemp,TEXT("AdamagedActor::Takedamage--MaxHP=%f"),MaxHP);

8

9 actuallyApplIEd = FMath::Clamp(MaxHP,0.0f,100.0f);

10

11 return actuallyApplIEd;

12 }

VIEw Code

以上就是简单的使用UE4的伤害系统。

总结

以上是内存溢出为你收集整理的UE4【C++】子d伤害系统使用全部内容,希望文章能够帮你解决UE4【C++】子d伤害系统使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1264787.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存