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

运行后

相关推荐
加油码2 小时前
动静态库的链接底层原理(进阶版)
linux·c++
一木 之林2 小时前
三、内存管理、指针与堆栈
c语言·jvm·c++
初夏睡觉3 小时前
原创题目 - 空壳恋爱
c++·算法·dp·oi·信息学·原创题目·值得挑战的题目
间歇性努力持续性发呆的野生快乐选手3 小时前
stl容器使用和算法简介
数据结构·c++
沙盘客4 小时前
AFSIM 官方案例库全景与解读方法论
c++·经验分享·后端
_wxd6665 小时前
STL-stack与queue与priority_queue(容器适配器)
c++
zh路西法6 小时前
【玩转VLA具身智能机械臂】(二):MoveIt2 运动规划——架构、配置与 C++ 上手
c++·机械臂·路径规划·vla·planner·moveit2
Yfhonier6 小时前
【素数定理】素数个数的位数
c++·算法·素数
爱和冰阔落6 小时前
【Linux】Ctrl+C 到底做了什么?从键盘、kill 到 alarm,一次讲清信号的产生
linux·运维·c++·安卓
水饺编程7 小时前
第5章,[Win32 章节] :贝塞尔样条曲线(一)
c语言·c++·windows·visual studio