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

运行后

相关推荐
一个小猴子`17 分钟前
CUDA全局内存
c++·cuda编程
ysa05103018 分钟前
【板子】树上启发式合并
数据结构·c++·笔记·算法
Simple_core33 分钟前
cmake模板和问题总结
c++
劉宇雁2 小时前
C++ ASCII 3D无尽跑酷游戏
c++·游戏·3d
杜子不疼.2 小时前
【C++ 在线五子棋对战】- 会话管理模块实现
开发语言·c++
有点。2 小时前
C++深度优先搜索(DFS)的概念(一)
开发语言·c++·深度优先
旖-旎3 小时前
《LeetCode 978 最长湍流子数组 || LeetCode 139 单词拆分》
c++·算法·leetcode·动态规划
aaPIXa6223 小时前
C++大型项目模块化拆分实战记录
开发语言·c++
石山代码3 小时前
C++23 新特性在 CLion 中的实战体验
开发语言·c++·c++23
在线OJ的阿川3 小时前
仿muduo库实现高并发服务器
linux·服务器·c++