欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-30 19:06:09
可能很多初学者如果想知道UE4的这种伤害系统如何使用起来会有点迷茫。 我自己做了demo给大家参考下。 首先要有个子弹类。 .h代码如下 1 UCLASS() 2 class SNVRCLAYSHOOTING_API AProjectile : public AActor 3 { 4 GENERAT ......

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

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

首先要有个子弹类。

.h代码如下

UE4【C++】子弹伤害系统使用
 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, blueprintreadwrite, category = "projectile")
14         uparticlesystemcomponent* launchparticle;    //烟火,开枪时
15 
16     uproperty(visibleanywhere, blueprintreadwrite, 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代码如下

UE4【C++】子弹伤害系统使用
 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<uprojectilemovementcomponent>(fname("projectilemovement"));    //生成组件
 7     projectilemovementcomponent->bautoactivate = false;        //自动飞行调成false
 8 
 9     projectilemesh = createdefaultsubobject<ustaticmeshcomponent>(fname("projectilemesh"));
10     rootcomponent = projectilemesh;
11     projectilemesh->setnotifyrigidbodycollision(true);
12     projectilemesh->setvisibility(true);
13 
14     launchparticle = createdefaultsubobject<uparticlesystemcomponent>(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, aactor* otheractor, uprimitivecomponent* othercomp, fvector normalimpulse, 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,null, udamagetype::staticclass());
51 
52     destroy();
53 }
view code

伤害的入口从onhit看出,调用了ugameplaystatics::applypointdamage;

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

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

每个actor都有这个接口virtual float takedamage(float damageamount, struct fdamageevent const& damageevent, class acontroller* eventinstigator, aactor* damagecauser);

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

受伤害源部分代码如下:

UE4【C++】子弹伤害系统使用
 1 float adamagedactor::takedamage(float damageamount, struct fdamageevent const& damageevent, class acontroller* eventinstigator, aactor* damagecauser)
 2 {
 3     float actuallyapplied = 0.0f;
 4 
 5     maxhp -= damageamount;
 6 
 7     ue_log(logtemp,warning,text("adamagedactor::takedamage--maxhp=%f"),maxhp);
 8 
 9     actuallyapplied = fmath::clamp<float>(maxhp, 0.0f, 100.0f);
10 
11     return actuallyapplied;
12 }
view code

 

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