新建MyPawn C++类
.h
#include "GameFramework/SpringArmComponent.h" //SpringArm组件 #include "Camera/CameraComponent.h" //Camera组件 class 工程名称_API AMyPawn : public APawn {//定义组件变量 public: UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent"); USceneComponent* MyRoot; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent"); USpringArmComponent* MySpringArm; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MySceneComponent"); UCameraComponent* MyCamera; }
.cpp
AMyPawn::AMyPawn() { // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true; //初始化组件变量的值 MyRoot = CreateDefaultSubobject(TEXT("MyRoot")); MySpringArm = CreateDefaultSubobject (TEXT("MySprintArm")); MyCamera = CreateDefaultSubobject (TEXT("MyCamera")); //设置父子层级关系 RootComponent = MyRoot; MySpringArm->SetupAttachment(MyRoot); MyCamera->SetupAttachment(MySpringArm); MySpringArm->bDoCollisionTest = false; //关闭DoCollisionTest } void AMyPawn::BeginPlay() {Super::BeginPlay(); //设置MyPawn的Transform FVector MyLocation = FVector(0,0,0); FRotator MyRotation = FRotator(-50,0,0); FVector MyScale = FVector(1,1,1); SetActorLocation(MyLocation); SetActorRotation(MyRotation); SetActorScale3D(MyScale); }
运行后