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

运行后

相关推荐
bkspiderx12 分钟前
C/C++中float浮点型的存储方式与使用要点
c++
起个名字费劲死了1 小时前
QT + Socket 客户端/服务端 公网通讯
服务器·c++·qt·socket
我是一只小青蛙8881 小时前
位图与布隆过滤器:高效数据结构解析
开发语言·c++·算法
xiaoye-duck1 小时前
吃透C++类和对象(下):初始化列表深度解析
c++
曼巴UE52 小时前
UE5 C++ GameInstanceSubsystem 在学习
c++·ue5·ue
Ethan Wilson2 小时前
VS2019 C++20 模块相关 C1001: 内部编译器错误
开发语言·c++·c++20
m0_748252382 小时前
Bootstrap 5 加载效果实现方法
c++
人工智能AI技术3 小时前
GitHub Copilot 2026新功能实操:C++跨文件上下文感知开发,效率翻倍技巧
c++·人工智能
大志若愚YYZ3 小时前
ROS2学习 C++中的this指针
c++·学习·算法
玖釉-4 小时前
[Vulkan 学习之路] 16 - 最终章:渲染循环与同步 (Rendering & Presentation)
c++·windows·图形渲染