• 02c++呵呵老师【官方案例FloatingActor】


    为actor添加一个静态网格体组件变量,类型是public的

    1. UPROPERTY(VisibleAnywhere) //这样才能让虚幻编辑器看到这个变量(公开)
    2. UStaticMeshComponent* VisualMesh;//StaticMeshComponent,它将担当对象的视觉表示

    在构造函数里,我们对指针变量进行赋值。

    1. //创建一个组件,并将该组件的地址赋给指针变量
    2. VisualMesh = CreateDefaultSubobject(TEXT("Mesh"));
    3. //将该组件附属在根组件上面
    4. VisualMesh->SetupAttachment(RootComponent);

    在这里我们看一下CreateDefaultSubobject的代码

    1. //这是一个模板类,并且返回一个指针
    2. template<class TReturnType>
    3. TReturnType* CreateDefaultSubobject(FName SubobjectName, bool bTransient = false)
    4. {
    5. UClass* ReturnType = TReturnType::StaticClass();
    6. return static_cast(CreateDefaultSubobject(SubobjectName, ReturnType, ReturnType, /*bIsRequired =*/ true, bTransient));
    7. }

    现在在资源文件夹下,找到相关资源

    1. static ConstructorHelpers::FObjectFinder CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));
    2. if (CubeVisualAsset.Succeeded())
    3. {
    4. //如果成功找到,就将该资源赋给网格体
    5. VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
    6. VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
    7. }

    tick()函数中,写功能,希望actor可以自转和上下浮动

    1. //获得actor的位置
    2. FVector NewLocation = GetActorLocation();
    3. //获得actor的旋转角度
    4. FRotator NewRotation = GetActorRotation();
    5. //获得物体创建时间
    6. float RunningTime = GetGameTimeSinceCreation();
    7. //获得一个高度
    8. float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    9. //高度放大20倍
    10. NewLocation.Z += DeltaHeight * 20.0f; //Scale our height by a factor of 20
    11. //得到一个旋转角度
    12. float DeltaRotation = DeltaTime * 20.0f; //Rotate by 20 degrees per second
    13. //得到绕z轴的旋转角度
    14. NewRotation.Yaw += DeltaRotation;
    15. //设置高度和旋转角度
    16. SetActorLocationAndRotation(NewLocation, NewRotation);

    编译,然后创建蓝图

     测试成功。

  • 相关阅读:
    使用pytorch搭建MobileNetV2并基于迁移学习训练
    BGP的基本配置
    蓝桥杯 (猜生日、棋盘放麦子、MP3储存 C++)
    ByteArray转byte[]的两种方式
    Spring Cloud LoadBalancer--负载均衡的原理(源码分析)
    32~python openpyxl 读取excel
    ICT产业关联效应的国际比较——基于投入产出的分析
    [LeetCode]1413. 逐步求和得到正数的最小值
    常见数据类型的占用字节数以及类型转换需要注意的事项
    AI_Neural Network_Note(一)
  • 原文地址:https://blog.csdn.net/zhang2362167998/article/details/126539281