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

运行后

相关推荐
仰泳的熊猫21 小时前
1094 The Largest Generation
数据结构·c++·算法·pat考试
獭.獭.21 小时前
C++ -- 位图与布隆过滤器
开发语言·c++
小年糕是糕手21 小时前
【C++】string类(二)
开发语言·数据结构·c++·程序人生·算法·leetcode·数字货币
福尔摩斯张21 小时前
Linux Kernel 设计思路与原理详解:从“一切皆文件“到模块化架构(超详细)
java·linux·运维·开发语言·jvm·c++·架构
傅里叶的耶21 小时前
C++ Primer Plus(第6版):第四章 复合类型
开发语言·c++
承渊政道1 天前
C++学习之旅【C++内存管理、模板初阶以及STL简介】
c++·学习·visual studio
郝学胜-神的一滴1 天前
使用Qt OpenGL开发俄罗斯方块:从零到一实现经典游戏
c++·qt·程序人生·游戏·设计模式·系统架构·图形渲染
Elnaij1 天前
从C++开始的编程生活(15)——模板知识补充
开发语言·c++
cccyi71 天前
高级I/O编程:从Select、Poll到Epoll的演进与Reactor服务器实现
服务器·c++·reactor
Elnaij1 天前
从C++开始的编程生活(16)——继承
开发语言·c++