• 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);

    编译,然后创建蓝图

     测试成功。

  • 相关阅读:
    [Linux入门]---Linux指令②
    Greenplum常用sql语句
    安装grafana
    【每日一题】补档 ABC308E - MEX | 遍历保存 |简单
    Android加固为何重要?很多人不学
    html总结-1-结构与常见标签
    Docker CI
    前端工程搭建:
    财务RPA机器人真的能提高效率吗?
    数据分析(python)学习笔记1.0
  • 原文地址:https://blog.csdn.net/zhang2362167998/article/details/126539281