在.h文件添加头文件,弹簧臂组件和相机组件
#include"GameFramework/SpringArmComponent.h"
#include"Camera/CameraComponent.h"
然后声明组件
public:
//场景组件,作为根主键
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
USceneComponent* MyRoot;
//摄像机摇臂组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
USpringArmComponent* MySpringArm;
//相机组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent")
UCameraComponent* MyCamera;
在cpp文件中,定义了三个函数,根,弹簧臂,相机
MyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("MyRootComponent"));
MySpringArm = CreateDefaultSubobject<USpringArmComponent(TEXT("MySpringArmComponent"));
MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MySCameraComponent"));
设置父子关系,将摇杆臂放在根组件下,然后将相机放在摇杆臂里面
RootComponent = MyRoot;
MySpringArm->SetupAttachment(MyRoot);
//将相机放在摄像机摇臂组件下
MyCamera->SetupAttachment(MySpringArm);
//设置碰撞,设置为false
MySpringArm->bDoCollisionTest=false;
像这样
然后在beginpaly设置位置
//设置位置
FVector MyLocation = FVector(0, 0, 0);
//设置旋转
FRotator MyRotation = FRotator(-50, 0, 0);
//设置缩放
FVector MyScale = FVector(1, 1, 1);
SetActorLocation(MyLocation);
SetActorRotation(MyRotation);
SetActorScale3D(MyScale);