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

运行后

相关推荐
是main不是漫7 小时前
【快慢指针 | LeetCode202 快乐数】鸽巢原理佐证环特性,快慢指针巧解快乐数
c语言·开发语言·c++·算法
Sashtok11 小时前
cpp基础知识(10)
c++·后端
whitelbwwww12 小时前
CUDA C++实战--中级
开发语言·前端·c++
ShineWinsu15 小时前
对于Qt:QMessageBox的解析
开发语言·c++·qt·面试·客户端·qmessagebox·消息对话框
小灰灰搞电子18 小时前
C++ initializer_list 详解
c++·list
Rabitebla20 小时前
【Linux】文件篇(六):Shell 实战——从系统调用到命令行重定向
linux·服务器·c++·笔记·学习
无忧.芙桃1 天前
C++语言原理与实践(二十三):哈希表的实现
c++·链表·哈希算法·散列表·哈希表
AvocadoZeroOneOI2 天前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
whitelbwwww2 天前
cuBLAS--CUDA高性能线性代数计算库--第一部分
c++·线性代数·gpu算力·cuda c++
eggwyw2 天前
C++设计模式之解释器模式
开发语言·c++