ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

UE4 C++创建摄像机摇臂和相机并且设置Transform

UE4 C++创建摄像机摇臂和相机并且设置Transform

新建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<USceneComponent>(TEXT("MyRoot"));MySpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("MySprintArm"));MyCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("MyCamera"));//设置父子层级关系RootComponent = MyRoot;MySpringArm->SetupAttachment(MyRoot);MyCamera->SetupAttachment(MySpringArm);MySpringArm->bDoCollisionTest = false;	//关闭DoCollisionTest
}void AMyPawn::BeginPlay()
{Super::BeginPlay();//设置MyPawn的TransformFVector MyLocation = FVector(0,0,0);FRotator MyRotation = FRotator(-50,0,0);FVector MyScale = FVector(1,1,1);SetActorLocation(MyLocation);SetActorRotation(MyRotation);SetActorScale3D(MyScale);
}

运行后
在这里插入图片描述

返回列表