• UE5 C++ 发射子弹发射(Projectile)


    一.相关蓝图的练习,在我之前的文章中射击子弹案例-CSDN博客

    本篇使用C++实现

    1.创建C++类 MyBullet,在MyBullet.h中包含相关头文件

    1. #include "CoreMinimal.h"
    2. #include "GameFramework/Actor.h"
    3. #include "Components/StaticMeshComponent.h" //模型组件
    4. //#include "Components/CapsuleComponent.h" //球形碰撞组件的头文件
    5. #include "GameFramework/ProjectileMovementComponent.h" //射击组件
    6. #include //球形碰撞组件的头文件
    7. #include "MyBullet.generated.h"


    2.声明属性变量,子弹模型,子弹碰撞体,子弹发射器

    1. public:
    2. UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MyComponent")
    3. UStaticMeshComponent* BulletMesh; //static 组件
    4. UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MyComponent")
    5. USphereComponent* MySphere; //碰撞组件
    6. UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MyComponent")
    7. UProjectileMovementComponent* FireGunProjectile; //

    3.将组件实例化

    1. BulletMesh = CreateDefaultSubobject(TEXT("BulletComponent")); //实例化StaticMesh组件
    2. //RootComponent =
    3. FireGunProjectile = CreateDefaultSubobject(TEXT("FireGunProjectileComponent")); //实例化ProjectileMovement组件
    4. MySphere = CreateDefaultSubobject(TEXT("SphereCollision")); //

    4.通过寻找静态模型,设置静态模型的实例。设置根组件,设置父子级,只要Projectile在根组件后面就能,让物体跟随,就有发射根组件的效果。

    再设置参数,调整效果。发射的初始速度,最大速度,重力等。

    1. MySphere = CreateDefaultSubobject(TEXT("SphereCollision")); //
    2. static ConstructorHelpers::FObjectFinderTmpStaticMesh(TEXT("/Script/Engine.StaticMesh'/Engine/BasicShapes/Sphere.Sphere'")); //静态加载资源 重要
    3. BulletMesh->SetStaticMesh(TmpStaticMesh.Object); //.Object重要
    4. RootComponent = BulletMesh;
    5. //FireGunProjectile->setupattack
    6. MySphere->SetupAttachment(RootComponent);
    7. MySphere->InitSphereRadius(67);
    8. FireGunProjectile->SetUpdatedComponent(RootComponent);//
    9. FireGunProjectile->InitialSpeed = 1200.0f;//初始速度
    10. FireGunProjectile->MaxSpeed = 24000.0f; //最大速度
    11. FireGunProjectile->bRotationFollowsVelocity = false; //旋转跟随重力
    12. FireGunProjectile->bIsHomingProjectile = true; //跟随组件
    13. FireGunProjectile->ProjectileGravityScale = 0.02; //设置重力

    5.生成蓝图类放到场景中,这样就能实现单发的子弹效果

  • 相关阅读:
    什么是消息中间件
    神光秘诀React
    【亲测有效】C盘容量满了,给C盘扩容!!!
    关于AES加密输出密文不为128位的倍数的原因
    【BM2 链表内指定区间反转】
    【设计模式】Java设计模式 - 外观模式
    Java 设计模式——中介者模式
    [人工智能-综述-15]:第九届全球软件大会(南京)有感 -4-大语言模型全流程、全方面提升软件生产效能
    9 二叉树的重建--来源于沈钰S同学(舒姐)
    使用Harbor搭建Docker仓库
  • 原文地址:https://blog.csdn.net/m0_64217692/article/details/136320573