1.1添加头文件(.h)
#include "GameFramework/SpringArmComponent.h"//弹簧臂组件
#include "Camera/CameraComponent.h"//摄像机组件
1.2创建变量(.h)
//创建场景组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)
USceneComponent* MyRoot;
//创建弹簧臂组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)
USpringArmComponent* MySpringArm;
//创建摄像机组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MySceneComponent)
UCameraComponent* MyCamera;
1.3在构造函数里初始化组件
//初始化组件
MyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("MyRootComponent"));
MySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySpringArmComponent"));
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCameraComponent"));
RootComponent = MyRoot;//设置为根组件