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

新建MyPawn C++类

.h

cpp 复制代码
#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

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的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);
}

运行后

相关推荐
PingdiGuo_guo2 分钟前
C++智能指针的知识!
开发语言·c++
Chuncheng's blog5 分钟前
CentOS 7如何编译安装升级gcc至7.5版本?
linux·运维·c++·centos
愚润求学2 小时前
【C++】类型转换
开发语言·c++
@我漫长的孤独流浪2 小时前
数据结构测试模拟题(4)
数据结构·c++·算法
csdnzzt3 小时前
从内存角度透视现代C++关键特性
c++
jie188945758663 小时前
C++ 中的 const 知识点详解,c++和c语言区别
java·c语言·c++
明月*清风3 小时前
c++ —— 内存管理
开发语言·c++
西北大程序猿4 小时前
单例模式与锁(死锁)
linux·开发语言·c++·单例模式
qq_454175794 小时前
c++学习-this指针
开发语言·c++·学习
超闻逸事6 小时前
【题解】[UTPC2024] C.Card Deck
c++·算法