1.创建一个空的c++项目
2.添加飞行器类,继承于pwan
3.添加静态组件
- UPROPERTY(VisibleAnywhere)
- class UStaticMeshComponent* Mesh;
4.在源文件中初始化
#include "Components/StaticMeshComponent.h"
- Mesh = CreateDefaultSubobject
(TEXT("Mesh")); - RootComponent = Mesh;
5.创建飞行器的模型
6. 此时导入无人机模型
点击导入
7.打开无人机蓝图,选择无人机模型
8.导入螺旋桨模型
9.为无人机的网格体添加四个插槽,装螺旋桨
10.添加插槽
11.关掉吸附功能,比较好操作
12.在顶视图设置位置
13.切换到前视图,继续调整位置
14.依次添加三个插槽
15.在c++的无人机类里面,为螺旋桨添加四个静态网格体变量
- //螺旋桨
- UPROPERTY(VisibleAnywhere)
- UStaticMeshComponent* Paddle1;
-
- UPROPERTY(VisibleAnywhere)
- UStaticMeshComponent* Paddle2;
-
- UPROPERTY(VisibleAnywhere)
- UStaticMeshComponent* Paddle3;
-
- UPROPERTY(VisibleAnywhere)
- UStaticMeshComponent* Paddle4;
16.在源文件里初始化
- //生成螺旋桨组件
- Paddle1 = CreateDefaultSubobject
(TEXT("Paddle1")); - Paddle2 = CreateDefaultSubobject
(TEXT("Paddle2")); - Paddle3 = CreateDefaultSubobject
(TEXT("Paddle3")); - Paddle4 = CreateDefaultSubobject
(TEXT("Paddle4")); - //绑定根组件,设置插槽位置
- Paddle1->SetupAttachment(Mesh, TEXT("Paddle1"));
- Paddle2->SetupAttachment(Mesh, TEXT("Paddle2"));
- Paddle3->SetupAttachment(Mesh, TEXT("Paddle3"));
- Paddle4->SetupAttachment(Mesh, TEXT("Paddle4"));
17.在无人机蓝图里设置螺旋桨
18.为了无人机添加碰撞盒子,在头文件里声明变量
- //碰撞盒子
- UPROPERTY(VisibleAnywhere)
- class UBoxComponent* OutCollision;
19.在构造函数中,进行初始化设置
- //碰撞盒子,并设为根组件
- OutCollision = CreateDefaultSubobject
(TEXT("BoxComponent")); - //设为根组件
- RootComponent = OutCollision;
- //设置大小
- OutCollision->SetBoxExtent(FVector(100.0f, 100.0f, 100.0f));
- //激活物理特性
- OutCollision->SetSimulatePhysics(true);
- //不希望盒子一直是水平的
- OutCollision->BodyInstance.bLockXRotation = true;
- OutCollision->BodyInstance.bLockYRotation = true;
- //机身组件
- Mesh = CreateDefaultSubobject
(TEXT("Mesh")); - //绑定到根组件
- Mesh->SetupAttachment(RootComponent);
20.编译,然后打开蓝图
21.修改盒体形状,移动无人机位置
22.碰撞预设设为pwan
23.设置质量
24. 现在计算一下无人机的重量,重力加速度是-9.8,质量是1kg,重量是其乘积
25.在头文件中增加向前和向上的推进器组件
- //向上推进器组件
- UPROPERTY(VisibleAnywhere)
- class UPhysicsThrusterComponent* UpThruster;
-
- //向前推进器组件
- UPROPERTY(VisibleAnywhere)
- class UPhysicsThrusterComponent* ForwardThruster;
26.在源文件中引入头文件
- #include "PhysicsEngine/PhysicsThrusterComponent.h"
- #include "Kismet/KismetMathLibrary.h"
27.在构造函数中初始设置
- UpThruster = CreateDefaultSubobject
(TEXT("UpThruster")); - UpThruster->SetupAttachment(RootComponent);
- UpThruster->ThrustStrength = 980.0f;
- UpThruster->SetAutoActivate(true);
- //x轴指向无人机下方
- UpThruster->SetWorldRotation(UKismetMathLibrary::MakeRotFromX(-this->GetActorUpVector()));
-
- ForwardThruster = CreateDefaultSubobject
(TEXT("ForwardThruster")); - ForwardThruster->SetupAttachment(RootComponent);
- ForwardThruster->ThrustStrength = 0.0f;
- ForwardThruster->SetAutoActivate(true);
- //用法同上
- ForwardThruster->SetWorldRotation(UKismetMathLibrary::MakeRotFromX(-this->GetActorForwardVector()));
28.增加无人机的空气阻力