本文主要讲述如何利用 Unreal Engine 来创建一个动态的立方体。
工具:
- Unreal Engine 4.27.2
- Visual Studio 2022
1、添加C++类
1:点击“文件”
2:“创建新C++类”
2、添加父类
1:选择 “Actor”
2:点击“下一步”
3、命名"Actor"
1:命名为“CubeActor”,因为一会我要创建一个立方体
2:点击“创建类”
4、编辑C++
点击“创建类”之后,等待一会儿,会启动 visual studio ,如下图所示。
1、编辑 CubeActor.h 文件
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CubeActor.generated.h"
UCLASS()
class STUDYBLUEPRINT_API ACubeActor : public AActor
{
GENERATED_BODY()
public:
// 定义初始浮动速度,并且设置这个速度可以在编辑器或者蓝图下修改
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CubeActor")
float FloatSpeed = 20.0f;
// 定义初始旋转速度,并且设置这个速度可以在编辑器或者蓝图下修改
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CubeActor")
float RotationSpeed = 20.0f;
// 定义 VisualMesh 属性,使其在编辑器的属性面板中可见,但不能被编辑。
UPROPERTY(VisibleAnywhere)
UStaticMeshComponent* VisualMesh;
// Sets default values for this actor's properties
ACubeActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
2、编辑 CubeActor.cpp 文件
// Fill out your copyright notice in the Description page of Project Settings.
#include "CubeActor.h"
// Sets default values
ACubeActor::ACubeActor()
{
// 创建组件(看得见的物体)函数,生成一个 Actor 的实例,使其与 unreal 系统兼容
VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
// 将 VisualMesh 添加 Actor 的根组件:位置、旋转、缩放
VisualMesh->SetupAttachment(RootComponent);
// 加载形状
static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));
// 判断是否生成 立方体
if (CubeVisualAsset.Succeeded())
{
// 将查找到的立方体组件加载到 VisualMesh 中
VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
// 设置组件 VisualMesh 的位置在父组件的中心点或原点位置
VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
}
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ACubeActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACubeActor::Tick(float DeltaTime)
{
// 定义一个向量,用于存放立方体组件 CubeActor 的位置信息
FVector NewLocation = GetActorLocation();
// 定义一个向量,用于存放立方体组件 CubeActor 的旋转信息
FRotator NewRotation = GetActorRotation();
// 获取当前 CubeActor 自创建以来经过的时间(以秒为单位)
float RunningTime = GetGameTimeSinceCreation();
// 计算这两个时点的正弦值差,这个差值表示在最近的一帧时间内正弦波的变化量。
float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
// 更新高度;按FloatSpeed调整高度
NewLocation.Z += DeltaHeight * FloatSpeed;
// 每秒旋转等于RotationSpeed的角度
float DeltaRotation = DeltaTime * RotationSpeed;
// 更新旋转的角度
NewRotation.Yaw += DeltaRotation;
// 设置立方体的高度以及旋转的角度
SetActorLocationAndRotation(NewLocation, NewRotation);
Super::Tick(DeltaTime);
}
5、设置启动项
右键“StudyBlueprint”,选择其“设为启动项目”
6、运行
1:选择“DebugGame Editor”
2:选择“Win64”
3:点击“本地 Windows 调试器”
7、放置立方体组件
等待一段时间,unreal engine 启动,如下图所示。此时 unreal 里面还没有立方体。
1、打开文件
点击这个小图标
2、放置立方体
1:打开C++类,点击“StudyBlueprint”
2:拖动"CubeActor"到仿真环境中
如下图所示:
3、设置立方体的初始位置
1:找到下图的这个位置,将 “位置” 这一行的 “X” 设置为 “-180” , “Y” 为 “0” , “Z” 为 “-180”;
2:点击“运行”。就会看到一个立方体上下漂浮并旋转。