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

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 propertiesABallPlayer();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:	// Called every framevirtual void Tick(float DeltaTime) override;// Called to bind functionality to inputvirtual 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 propertiesABallPlayer();protected:// Called when the game starts or when spawnedvirtual 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 inputvirtual 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

相关文章:

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

UE5视频看了不少&#xff0c;但基本都是蓝图如何搞&#xff0c;或者改一下属性&#xff0c;理解UE系统现有组件使用的。一直对C脚本和蓝图之间的关系不是很理解&#xff0c;看到一个视频讲的很好&#xff0c;我也做笔记记录一下。 我的环境是UE5.3.2. 创建UE空项目 我们创建…...

使用FreeNAS软件部署ISCSI的SAN架构存储(IP-SAN)练习题

一&#xff0c;实验用到工具分别为&#xff1a; VMware虚拟机&#xff0c;安装教程&#xff1a;VMware Workstation Pro 17 安装图文教程 FreeNAS系统&#xff0c;安装教程&#xff1a;FreeNAS-11.2-U4.1安装教程2024&#xff08;图文教程&#xff09; 二&#xff0c;新建虚…...

Sql Sqserver 相关知识总结

Sql Sqserver 相关知识总结 文章目录 Sql Sqserver 相关知识总结前言优化语句查询&#xff08;select&#xff09;条件过滤&#xff08;Where&#xff09;分组处理&#xff08;GROUP BY&#xff09;模糊查询&#xff08;like&#xff09;包含&#xff08;in&#xff09;合集&am…...

面试题整理17----K8s中request和limit资源限制是如何实现的

面试题整理17----K8s中request和limit资源限制是如何实现的 1. 资源请求&#xff08;Resource Requests&#xff09;2. 资源限制&#xff08;Resource Limits&#xff09;3. 总结 在Kubernetes&#xff08;K8s&#xff09;中&#xff0c;Pod的资源限制&#xff08;Resource Lim…...

Spring Boot @Conditional注解

在Spring Boot中&#xff0c;Conditional 注解用于条件性地注册bean。这意味着它可以根据某些条件来决定是否应该创建一个特定的bean。这个注解可以放在配置类或方法上&#xff0c;并且它会根据提供的一组条件来判断是否应该实例化对应的组件。 要使用 Conditional注解时&#…...

jpeg文件学习

相关最全的一篇文章链接&#xff1a;https://www.cnblogs.com/wtysos11/p/14089482.html YUV基础知识 Y表示亮度分量&#xff1a;如果只显示Y的话&#xff0c;图像看起来会是一张黑白照。 U&#xff08;Cb&#xff09;表示色度分量&#xff1a;是照片蓝色部分去掉亮度&#x…...

c++基于过程

前言&#xff1a; 笔记基于C黑马程序员网课视频&#xff1a;黑马程序员匠心之作|C教程从0到1入门编程,学习编程不再难_哔哩哔哩_bilibili 在此发布笔记&#xff0c;只是为方便学习&#xff0c;不做其他用途&#xff0c;原作者为黑马程序员。 1. C基础 1.1 用Visual Studio写C程…...

FOC软件 STM32CubeMX 使用

1、安装-及相关软件版本 展示版本注意事项:keil MDK和STM32CubeMX版本至少要大于等于图中版本。 2、 Motor Profiler 5.2.0使用方法...

leetcode hot 100 全排列

46. 全排列 已解答 中等 相关标签 相关企业 给定一个不含重复数字的数组 nums &#xff0c;返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。 class Solution(object): def permute(self, nums): """ :type nums: List[int] :rtype: List[List[int…...

使用qrcode.vue生成当前网页的二维码(H5)

使用npm&#xff1a; npm install qrcode.vue 使用yarn&#xff1a; yarn add qrcode.vue package.json&#xff1a; 实现&#xff1a; <template><div class"code"><qrcode-vue :value"currentUrl" :size"size" render-as&…...

0055. shell命令--useradd

目录 55. shell命令--useradd 功能说明 语法格式 选项说明 选项 退出值 相关文件 /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/skel/ /etc/login.defs /etc/default/useradd 实践操作 注意事项 55. shell命令--useradd 功能说明 useradd 命令是 Lin…...

blender中合并的模型,在threejs中显示多个mesh;blender多材质烘培成一个材质

描述&#xff1a;在blender中合并的模型导出为glb&#xff0c;在threejs中导入仍显示多个mesh&#xff0c;并不是统一的整体&#xff0c;导致需要整体高亮或者使用DragControls等不能统一控制。 原因&#xff1a;模型有多个材质&#xff0c;在blender中合并的时候&#xff0c;…...

vue 本地自测iframe通讯

使用 postMessage API 来实现跨窗口&#xff08;跨域&#xff09;的消息传递。postMessage 允许你安全地发送消息到其他窗口&#xff0c;包括嵌套的 iframe&#xff0c;而不需要担心同源策略的问题。 发送消息&#xff08;父应用&#xff09; 1. 父应用&#xff1a;发送消息给…...

C++:单例模式

创建自己的对象&#xff0c;同时确保对象的唯一性。 单例类只能有一个实例☞静态成员static☞静态成员 必须类外初始化 单例类必须自己创建自己的唯一实例 单例类必须给所有其他对象提供这一实例 静态成员类内部可以访问 构造函数私有化☞构造函数私有外部不能创建&#x…...

SOME/IP 协议详解——信息格式

文章目录 1. 头部格式1.1 消息 ID&#xff08;Message ID&#xff09;1.2 长度&#xff08;Length&#xff09;1.3 请求 ID&#xff08;Request ID&#xff09;1.4 协议版本&#xff08;Protocol Version&#xff09;&#xff1a;1.5 接口版本&#xff08;Interface Version&am…...

C# GDI+数码管数字控件

调用方法 int zhi 15;private void button1_Click(object sender, EventArgs e){if (zhi > 19){zhi 0;}lcdDisplayControl1.DisplayText zhi.ToString();} 运行效果 控件代码 using System; using System.Collections.Generic; using System.Drawing.Drawing2D; using …...

在交叉编译中,常见的ELF(elf)到底是什么意思?

ELF 是 Executable and Linkable Format 的缩写&#xff0c;中文翻译为“可执行与可链接格式”。它是一种通用的文件格式&#xff0c;主要用于存储可执行文件、目标文件&#xff08;编译后的中间文件&#xff09;、动态库&#xff08;.so 文件&#xff09;以及内存转储文件&…...

Unity开发AR之Vuforia-MultiTarget笔记

前言 在增强现实(AR)技术蓬勃发展的今天,越来越多的开发者开始探索如何将AR应用于各种场景中。Vuforia作为一个领先的AR开发平台,为开发者提供了强大的工具和功能,使得创建AR体验变得更加简单和直观。本文将为您介绍Vuforia的基本概念、特点,以及如何配置和使用MultiTar…...

深入解析 Oracle 的聚合函数 ROLLUP

目录 深入解析 Oracle 的聚合函数 ROLLUP一、ROLLUP 函数概述二、ROLLUP 函数语法三、ROLLUP 实例详解&#xff08;一&#xff09;基础分组聚合&#xff08;二&#xff09;引入 ROLLUP 函数&#xff08;三&#xff09;ROLLUP 与 NULL 值&#xff08;四&#xff09;多列复杂分组…...

Wend看源码-Java-集合学习(List)

摘要 本篇文章深入探讨了基于JDK 21版本的Java.util包中提供的多样化集合类型。在Java中集合共分类为三种数据结构&#xff1a;List、Set和Queue。本文将详细阐述这些数据类型的各自实现&#xff0c;并按照线程安全性进行分类&#xff0c;分别介绍非线程安全与线程安全的实现方…...

K8S认证|CKS题库+答案| 11. AppArmor

目录 11. AppArmor 免费获取并激活 CKA_v1.31_模拟系统 题目 开始操作&#xff1a; 1&#xff09;、切换集群 2&#xff09;、切换节点 3&#xff09;、切换到 apparmor 的目录 4&#xff09;、执行 apparmor 策略模块 5&#xff09;、修改 pod 文件 6&#xff09;、…...

VB.net复制Ntag213卡写入UID

本示例使用的发卡器&#xff1a;https://item.taobao.com/item.htm?ftt&id615391857885 一、读取旧Ntag卡的UID和数据 Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click轻松读卡技术支持:网站:Dim i, j As IntegerDim cardidhex, …...

多场景 OkHttpClient 管理器 - Android 网络通信解决方案

下面是一个完整的 Android 实现&#xff0c;展示如何创建和管理多个 OkHttpClient 实例&#xff0c;分别用于长连接、普通 HTTP 请求和文件下载场景。 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas…...

(二)TensorRT-LLM | 模型导出(v0.20.0rc3)

0. 概述 上一节 对安装和使用有个基本介绍。根据这个 issue 的描述&#xff0c;后续 TensorRT-LLM 团队可能更专注于更新和维护 pytorch backend。但 tensorrt backend 作为先前一直开发的工作&#xff0c;其中包含了大量可以学习的地方。本文主要看看它导出模型的部分&#x…...

全球首个30米分辨率湿地数据集(2000—2022)

数据简介 今天我们分享的数据是全球30米分辨率湿地数据集&#xff0c;包含8种湿地亚类&#xff0c;该数据以0.5X0.5的瓦片存储&#xff0c;我们整理了所有属于中国的瓦片名称与其对应省份&#xff0c;方便大家研究使用。 该数据集作为全球首个30米分辨率、覆盖2000–2022年时间…...

最新SpringBoot+SpringCloud+Nacos微服务框架分享

文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的&#xff0c;根据Excel列的需求预估的工时直接打骨折&#xff0c;不要问我为什么&#xff0c;主要…...

基于当前项目通过npm包形式暴露公共组件

1.package.sjon文件配置 其中xh-flowable就是暴露出去的npm包名 2.创建tpyes文件夹&#xff0c;并新增内容 3.创建package文件夹...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院查看报告小程序

一、开发环境准备 ​​工具安装​​&#xff1a; 下载安装DevEco Studio 4.0&#xff08;支持HarmonyOS 5&#xff09;配置HarmonyOS SDK 5.0确保Node.js版本≥14 ​​项目初始化​​&#xff1a; ohpm init harmony/hospital-report-app 二、核心功能模块实现 1. 报告列表…...

三体问题详解

从物理学角度&#xff0c;三体问题之所以不稳定&#xff0c;是因为三个天体在万有引力作用下相互作用&#xff0c;形成一个非线性耦合系统。我们可以从牛顿经典力学出发&#xff0c;列出具体的运动方程&#xff0c;并说明为何这个系统本质上是混沌的&#xff0c;无法得到一般解…...

【论文阅读28】-CNN-BiLSTM-Attention-(2024)

本文把滑坡位移序列拆开、筛优质因子&#xff0c;再用 CNN-BiLSTM-Attention 来动态预测每个子序列&#xff0c;最后重构出总位移&#xff0c;预测效果超越传统模型。 文章目录 1 引言2 方法2.1 位移时间序列加性模型2.2 变分模态分解 (VMD) 具体步骤2.3.1 样本熵&#xff08;S…...