当前位置: 首页 > article >正文

UE(虚幻)学习(四) 第一个C++类来控制小球移动来理解蓝图和脚本如何工作

UE5视频看了不少,但基本都是蓝图如何搞,或者改一下属性,理解UE系统现有组件使用的。一直对C++脚本和蓝图之间的关系不是很理解,看到一个视频讲的很好,我也做笔记记录一下。

我的环境是UE5.3.2.

创建UE空项目

我们创建一个空项目
在这里插入图片描述

创建类MyPlayer

进入项目后,我们直接创建一个C++类。
在这里插入图片描述
因为是从基础理解,所以就选择更基础的Pawn类,不使用Character类。
在这里插入图片描述
我起名字MyPlayer类。确定后就提示会
在这里插入图片描述
之前创建的是蓝图项目,创建类后就包含源码了,需要关闭UE重新进入。

进入后我们就可以看到自己建立的类了。
在这里插入图片描述
如果你看不到C++Classes,请掉右边的Settings,并勾选Show C++ Classes。
在这里插入图片描述
这里我重新创建了类名BallPlayer,不喜欢之前的MyPlayer的名字,就不附上截图了。

查看新生成的文件

我们用VS打开项目,可以看到.h和.cpp文件,内容如下:

BallPlayer.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "BallPlayer.generated.h"

UCLASS()
class MYPROJECT4_API ABallPlayer : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	ABallPlayer();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

};


BallPlayer.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "BallPlayer.h"

// Sets default values
ABallPlayer::ABallPlayer()
{
 	// 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;

}

// Called when the game starts or when spawned
void ABallPlayer::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ABallPlayer::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void ABallPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

}



UE的类前面自动添加了A字母。继承于APawn。

修改代码

下面我们添加一些代码:
BallPlayer.h文件中添加了三个组件:模型Mesh,弹簧,相机。
Mesh就是我们控制看到的小球(UStaticMeshComponent),弹簧(USpringArmComponent)就是用来控制相机和小球之间的距离,相机(UCameraComponent)就是我们的画面视角。

然后MoveForce和JumpForce是控制的物理力大小。

方法:
MoveRight是用来处理左右移动的
MoveForward是用来处理前后移动的
Jump是跳跃

这里注意下头文件,引用的文件要写在#include "BallPlayer.generated.h"的前面。

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"

#include "BallPlayer.generated.h"


UCLASS()
class MYPROJECT4_API ABallPlayer : public APawn
{
	GENERATED_BODY()

public:
	// Sets default values for this pawn's properties
	ABallPlayer();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
	UStaticMeshComponent* MeshMain;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
	USpringArmComponent* SpringArmMain;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
	UCameraComponent* CameraMain;

	UPROPERTY(EditAnywhere,BlueprintReadWrite)
	float MoveForce = 500.00f;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	float JumpForce = 500.00f;

public:	
	// Called every frame
	//virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	void MoveRight(float value);
	void MoveForward(float value);
	void Jump();
};

我们再来看CPP文件
构造函数中我们看到创建了Mesh,SpringArm和Camera。
SetupAttachment的作用是设置父对象,例如SprintArm的父对象是Mesh。相机是最子层。

SetupPlayerInputComponent函数中绑定了按键,我们后面再截图上来。
BindAction,BindAxis 分别对应了不同的操作。

当键盘按下的时候,就进入(MoveRight,MoveForward,Jump)3个函数进行前后左右跳跃处理。

// Fill out your copyright notice in the Description page of Project Settings.


#include "BallPlayer.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"


// Sets default values
ABallPlayer::ABallPlayer()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;
	MeshMain = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
	SpringArmMain = CreateDefaultSubobject<USpringArmComponent>("SpringArm");
	CameraMain = CreateDefaultSubobject<UCameraComponent>("Camera");

	RootComponent = MeshMain;
	SpringArmMain->SetupAttachment(MeshMain);
	CameraMain->SetupAttachment(SpringArmMain);

	MeshMain->SetSimulatePhysics(true);
	SpringArmMain->bUsePawnControlRotation = true;
}

// Called when the game starts or when spawned
void ABallPlayer::BeginPlay()
{
	Super::BeginPlay();
	MoveForce *= MeshMain->GetMass();
	JumpForce *= MeshMain->GetMass();
}

 Called every frame
//void ABallPlayer::Tick(float DeltaTime)
//{
//	Super::Tick(DeltaTime);
//
//}

// Called to bind functionality to input
void ABallPlayer::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	//映射按键
	InputComponent->BindAction("Jump", IE_Pressed, this, &ABallPlayer::Jump);
	InputComponent->BindAxis("MoveForward", this,  &ABallPlayer::MoveForward);
	InputComponent->BindAxis("MoveRight", this,  &ABallPlayer::MoveRight);
}

void ABallPlayer::MoveRight(float value)
{
	const FVector right = CameraMain->GetRightVector() * MoveForce * value;
	MeshMain->AddForce(right);
}

void ABallPlayer::MoveForward(float value)
{
	const FVector forward = CameraMain->GetForwardVector() * MoveForce * value;
	MeshMain->AddForce(forward);
}

void ABallPlayer::Jump()
{
	MeshMain->AddImpulse(FVector(0, 0, JumpForce));
}


键盘的设置

我们打开菜单的编辑Edit 、项目设置Project Settings
在这里插入图片描述
代码编写好后可以按下Ctrl+Alt+F11,进行快速编译。

创建蓝图

我们的代码编译完成后,可以再脚本上右键创建蓝图。
在这里插入图片描述
或者可以再文件夹力点击右键创建蓝图:
在这里插入图片描述
在弹出界面上搜索ballplayer选择确定。
在这里插入图片描述
双击建立好的蓝图BP_BallPlayer,会进入蓝图界面,我们就看到Mesh弹簧和相机的层次结构了。
在这里插入图片描述

设置小球

蓝图里我们点击小球选择一个Mesh
在这里插入图片描述
这里选择的小球Mesh是100*100的。

设置弹簧

在这里插入图片描述
我们关闭几个参数inheritPitch等,按E切换到旋转,然后让相机有一些高度,这样视角舒服一些。

保存蓝图

最后保存蓝图,并点击Compile编译。
在这里插入图片描述

设置玩家

在场景中拖入BP_BallPlayer蓝图,并且设置Pawn里的AutoPossessPlayer的属性选择Player 0。
在这里插入图片描述

运行游戏

请添加图片描述

参考:
https://www.youtube.com/watch?v=KQgOqyYoHAs


http://www.kler.cn/a/455747.html

相关文章:

  • 使用Python实现智慧城市数据平台:走向未来的智能城市管理
  • 如何使用Python和PIL库生成带竖排文字的封面图像
  • IS-IS(Intermediate System to Intermediate System)
  • Peter Lax线性代数教材:Linear Algebra and Its Applications 2nd Ed
  • vue3项目使用scss报错相关处理
  • 使用Vue+Django开发的旅游路书应用
  • 专业版pycharm与服务器连接
  • HarmonyOS Next 应用元服务开发-应用接续动态配置迁移按需退出
  • 关于uni-forms组件的bug【提交的字段[‘*‘]在数据库中并不存在】
  • [Unity Shader][图形渲染] Shader数学基础11 - 复合变换详解
  • SpringBoot3整合MyBatisPlus时遇到的问题及解决办法
  • leetCode 292Nim游戏
  • 工作编码案例--UDP多播 和 本地套接字bind
  • 5.UE5横板2D游戏,摄像机移动和停止移动,
  • Linux下学【MySQL】表中插入和查询的进阶操作(配实操图和SQL语句通俗易懂)
  • NCR+可变电荷块——文献hub1
  • Ftrans数据摆渡系统 搭建安全便捷跨网文件传输通道
  • 如何循序渐进的学习人工智能
  • 国际版JAVA同城跑腿源码快递代取帮买帮送同城服务源码支持Android+IOS+H5
  • 使用sam进行零样本、零学习的分割实践