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

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

本篇使用C++实现

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

cpp 复制代码
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"  //模型组件
//#include "Components/CapsuleComponent.h"  //球形碰撞组件的头文件
#include "GameFramework/ProjectileMovementComponent.h"   //射击组件
#include <Components/SphereComponent.h> //球形碰撞组件的头文件
#include "MyBullet.generated.h" 

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

cpp 复制代码
public:
	UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MyComponent")
	UStaticMeshComponent* BulletMesh;   //static 组件
	UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MyComponent")
	USphereComponent* MySphere;    //碰撞组件
	UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "MyComponent")
	UProjectileMovementComponent* FireGunProjectile; //

3.将组件实例化

cpp 复制代码
BulletMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BulletComponent")); //实例化StaticMesh组件
//RootComponent = 
FireGunProjectile = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("FireGunProjectileComponent")); //实例化ProjectileMovement组件
MySphere = CreateDefaultSubobject<USphereComponent>(TEXT("SphereCollision")); // 

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

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

cpp 复制代码
MySphere = CreateDefaultSubobject<USphereComponent>(TEXT("SphereCollision")); // 
static ConstructorHelpers::FObjectFinder<UStaticMesh>TmpStaticMesh(TEXT("/Script/Engine.StaticMesh'/Engine/BasicShapes/Sphere.Sphere'")); //静态加载资源 重要
BulletMesh->SetStaticMesh(TmpStaticMesh.Object); //.Object重要

RootComponent = BulletMesh;

//FireGunProjectile->setupattack
MySphere->SetupAttachment(RootComponent);
MySphere->InitSphereRadius(67);
FireGunProjectile->SetUpdatedComponent(RootComponent);//
FireGunProjectile->InitialSpeed = 1200.0f;//初始速度
FireGunProjectile->MaxSpeed = 24000.0f; //最大速度
FireGunProjectile->bRotationFollowsVelocity = false; //旋转跟随重力
FireGunProjectile->bIsHomingProjectile = true;    //跟随组件
FireGunProjectile->ProjectileGravityScale = 0.02; //设置重力

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

相关推荐
平行云3 天前
Paraverse平行云实时云渲染助力第82届威尼斯电影节XR沉浸式体验
unity·云原生·ue5·xr·实时云渲染
一眼万里*e3 天前
用ai写了个UE5插件
c++·ue5
成都渲染101云渲染66664 天前
电脑配置不足怎么办,告别硬件束缚,川翔云电脑
ue5·电脑
m0_552200824 天前
《UE5_C++多人TPS完整教程》学习笔记47 ——《P48 瞄准行走(Aim Walking)》
c++·游戏·ue5
会思考的猴子8 天前
UE5 PCG 笔记(三) Normal To Density 节点
笔记·ue5
吴梓穆12 天前
UE5 UI遮罩
ue5
枯萎穿心攻击13 天前
Unity VS UE 性能工具与内存管理
开发语言·游戏·unity·ue5·游戏引擎·虚幻·虚幻引擎
黑客影儿13 天前
使用UE5开发2.5D开放世界战略养成类游戏的硬件配置指南
开发语言·c++·人工智能·游戏·智能手机·ue5·游戏引擎
幻雨様13 天前
UE5多人MOBA+GAS 54、用户登录和会话创建请求
android·ue5
吴梓穆14 天前
UE5 InVideo插件 使用
ue5