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

运行后

相关推荐
c238567 分钟前
# Mini OJ — 项目详解文档
开发语言·数据库·c++
c2385621 分钟前
C/C++每日一练20
c语言·开发语言·c++
可爱系程序猿1 小时前
msvcp140_codecvt_ids.dll 缺失的排查记录:C++ 程序启动异常时如何核对运行库版本
开发语言·c++·程序人生·电脑
encoconut1 小时前
01_C++ 入门基础
c++
郝学胜-神的一滴3 小时前
中级OpenGL教程 030:gl_FragCoord解锁区域渲染与深度可视化新姿势
c++·unity·游戏引擎·图形渲染·unreal engine·opengl
不会代码的小猴3 小时前
11. 类和动态内存分配
开发语言·c++
鱼子星_4 小时前
【C++】stack和queue的应用及其模拟实现:适配器与容器适配器
数据结构·c++·笔记·stl
jufeng13074 小时前
【系列:MiniKV 原理剖析 · 第 2 篇】
linux·c++·软件工程
charlie1145141915 小时前
IMX6ULL WM8960 的移植——前置介绍
开发语言·c++·开源项目·嵌入式linux
charlie1145141915 小时前
Cinux是如何管理进程的 —— 上下文与调度
开发语言·c++·操作系统·开源项目