• 01UEc++【打飞艇】


    1.创建一个空的c++项目

     2.添加飞行器类,继承于pwan


    3.添加静态组件

    1. UPROPERTY(VisibleAnywhere)
    2. class UStaticMeshComponent* Mesh;

     4.在源文件中初始化

    #include "Components/StaticMeshComponent.h"
    1. Mesh = CreateDefaultSubobject(TEXT("Mesh"));
    2. RootComponent = Mesh;

    5.创建飞行器的模型

      

    6. 此时导入无人机模型

    点击导入 

     

     

    7.打开无人机蓝图,选择无人机模型

     8.导入螺旋桨模型

     

    9.为无人机的网格体添加四个插槽,装螺旋桨

    10.添加插槽

    11.关掉吸附功能,比较好操作

    12.在顶视图设置位置

    13.切换到前视图,继续调整位置

    14.依次添加三个插槽

     15.在c++的无人机类里面,为螺旋桨添加四个静态网格体变量

    1. //螺旋桨
    2. UPROPERTY(VisibleAnywhere)
    3. UStaticMeshComponent* Paddle1;
    4. UPROPERTY(VisibleAnywhere)
    5. UStaticMeshComponent* Paddle2;
    6. UPROPERTY(VisibleAnywhere)
    7. UStaticMeshComponent* Paddle3;
    8. UPROPERTY(VisibleAnywhere)
    9. UStaticMeshComponent* Paddle4;

     16.在源文件里初始化

    1. //生成螺旋桨组件
    2. Paddle1 = CreateDefaultSubobject(TEXT("Paddle1"));
    3. Paddle2 = CreateDefaultSubobject(TEXT("Paddle2"));
    4. Paddle3 = CreateDefaultSubobject(TEXT("Paddle3"));
    5. Paddle4 = CreateDefaultSubobject(TEXT("Paddle4"));
    6. //绑定根组件,设置插槽位置
    7. Paddle1->SetupAttachment(Mesh, TEXT("Paddle1"));
    8. Paddle2->SetupAttachment(Mesh, TEXT("Paddle2"));
    9. Paddle3->SetupAttachment(Mesh, TEXT("Paddle3"));
    10. Paddle4->SetupAttachment(Mesh, TEXT("Paddle4"));

    17.在无人机蓝图里设置螺旋桨

     

     18.为了无人机添加碰撞盒子,在头文件里声明变量

    1. //碰撞盒子
    2. UPROPERTY(VisibleAnywhere)
    3. class UBoxComponent* OutCollision;

    19.在构造函数中,进行初始化设置

    1. //碰撞盒子,并设为根组件
    2. OutCollision = CreateDefaultSubobject(TEXT("BoxComponent"));
    3. //设为根组件
    4. RootComponent = OutCollision;
    5. //设置大小
    6. OutCollision->SetBoxExtent(FVector(100.0f, 100.0f, 100.0f));
    7. //激活物理特性
    8. OutCollision->SetSimulatePhysics(true);
    9. //不希望盒子一直是水平的
    10. OutCollision->BodyInstance.bLockXRotation = true;
    11. OutCollision->BodyInstance.bLockYRotation = true;
    12. //机身组件
    13. Mesh = CreateDefaultSubobject(TEXT("Mesh"));
    14. //绑定到根组件
    15. Mesh->SetupAttachment(RootComponent);

    20.编译,然后打开蓝图

     21.修改盒体形状,移动无人机位置

     22.碰撞预设设为pwan

    23.设置质量

    24. 现在计算一下无人机的重量,重力加速度是-9.8,质量是1kg,重量是其乘积 

    25.在头文件中增加向前和向上的推进器组件

    1. //向上推进器组件
    2. UPROPERTY(VisibleAnywhere)
    3. class UPhysicsThrusterComponent* UpThruster;
    4. //向前推进器组件
    5. UPROPERTY(VisibleAnywhere)
    6. class UPhysicsThrusterComponent* ForwardThruster;

    26.在源文件中引入头文件

    1. #include "PhysicsEngine/PhysicsThrusterComponent.h"
    2. #include "Kismet/KismetMathLibrary.h"

    27.在构造函数中初始设置

    1. UpThruster = CreateDefaultSubobject(TEXT("UpThruster"));
    2. UpThruster->SetupAttachment(RootComponent);
    3. UpThruster->ThrustStrength = 980.0f;
    4. UpThruster->SetAutoActivate(true);
    5. //x轴指向无人机下方
    6. UpThruster->SetWorldRotation(UKismetMathLibrary::MakeRotFromX(-this->GetActorUpVector()));
    7. ForwardThruster = CreateDefaultSubobject(TEXT("ForwardThruster"));
    8. ForwardThruster->SetupAttachment(RootComponent);
    9. ForwardThruster->ThrustStrength = 0.0f;
    10. ForwardThruster->SetAutoActivate(true);
    11. //用法同上
    12. ForwardThruster->SetWorldRotation(UKismetMathLibrary::MakeRotFromX(-this->GetActorForwardVector()));

    28.增加无人机的空气阻力

     

  • 相关阅读:
    Winsoft Office Component Suite
    Day58 739. 每日温度 496.下一个更大元素 I
    css实现价格降价线
    PART1 WebPack
    kafka
    Vue3 源码阅读(12):组件化 —— KeepAlive
    嵌入式IDE(1):IAR中ICF链接文件详解和实例分析
    【Axure教程】能增删改数据的动态饼图
    SAGAN
    完美解决mac git clone总是报128错误|git clone克隆问题
  • 原文地址:https://blog.csdn.net/zhang2362167998/article/details/126745309