UE5 C++ 跑酷游戏练习 Part1

一.修改第三人称模板的 Charactor

1.随鼠标将四处看的功能的输入注释掉。

void ARunGANCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{// Set up action bindingsif (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent)) {//JumpingEnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);//MovingEnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ARunGANCharacter::Move);//Looking//EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ARunGANCharacter::Look);}

2.在Tick里,注释掉计算左右方向的部分。只让它获得向前的方向。再加个每帧都朝,正前方输入的逻辑。

void ARunGANCharacter::Tick(float DeltaSeconds)
{Super::Tick(DeltaSeconds);GetController()->SetControlRotation(FMath::RInterpConstantTo(GetControlRotation(),DesireRotation,GetWorld()->GetRealTimeSeconds(),10.f));//const FRotator Rotation = Controller->GetControlRotation();const FRotator YawRotation(0, Rotation.Yaw, 0);// get forward vectorconst FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);// get right vector //const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);//AddMovementInput(ForwardDirection, 1);
}

3.修改按下输入,调用的Move里的逻辑。让它如果转向,就按自己输入的X方向。旋转90度。

如果不转向,按照人物控制当前的朝向,计算左右的向量,并添加左右输入。

相当于一直向前跑,不转向可以左右调整。

if (bTurn)
{ //GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,TEXT("Turn"));FRotator NewRotation = FRotator(0.f,90.f*(MovementVector.X), 0.f);FQuat QuatA = FQuat(DesireRotation);FQuat QuatB = FQuat(NewRotation);DesireRotation = FRotator(QuatA * QuatB);bTurn = false;
}
else
{// find out which way is forwardconst FRotator Rotation = Controller->GetControlRotation();const FRotator YawRotation(0, Rotation.Yaw, 0);// get forward vector//const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);// get right vector const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);//ForwardDirection = FVector(0,0,0);// add movement //AddMovementInput(ForwardDirection, 0);  //   AddMovementInput(RightDirection, MovementVector.X);/*	if (Controller->IsLocalPlayerController()){APlayerController* const PC = CastChecked<APlayerController>(Controller);}*///GetCharacterMovement()->bOrientRotationToMovement = false;//
}

4.这里转向的方向DesireRotation,会被一直赋值到控制器(Controller->GetControlRotation)。进而影响我们 向前方向。因为Tick里一直在修正。左右会在,Move回调函数里添加,也受控制器的影响。

void ARunGANCharacter::Tick(float DeltaSeconds)
{Super::Tick(DeltaSeconds);GetController()->SetControlRotation(FMath::RInterpConstantTo(GetControlRotation(),DesireRotation,GetWorld()->GetRealTimeSeconds(),10.f));//const FRotator Rotation = Controller->GetControlRotation();const FRotator YawRotation(0, Rotation.Yaw, 0);// get forward vectorconst FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);// get right vector //const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);//AddMovementInput(ForwardDirection, 1);
}

人物逻辑就写好了,一直跑,Turn为True时转90.其余时间,相当于一直按W,你自己决定要不要A,D,斜向跑。

二.写碰撞,碰撞时,能实现转向。

创建TurnBox C++ Actor类。在里面,添加UBoxComponent组件,添加碰撞的回调函数。内容也很简单,如果是 角色碰撞,让它的装箱变量变为True。

#include "TurnBox.generated.h"class UBoxComponent;
UCLASS()
class RUNGAN_API ATurnBox : public AActor
{GENERATED_BODY()UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = Box,meta = (AllowPrivateAccess = "true"))UBoxComponent* Box;public:	// Sets default values for this actor's propertiesATurnBox();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:	// Called every framevirtual void Tick(float DeltaTime) override;UFUNCTION()void CharacterOverlapStart(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool  bFromSweep, const FHitResult& SweepResult);// UPrimitiveComponent, OnComponentBeginOverlap, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex, bool, bFromSweep, const FHitResult &, SweepResult);UFUNCTION()void CharacterOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);//UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex);
};

这里实例化组件,就不写了。把角色的头文件,包含进去。绑定回调,回调里判断逻辑。

// Called when the game starts or when spawned
void ATurnBox::BeginPlay()
{Super::BeginPlay();	Box->OnComponentBeginOverlap.AddDynamic(this,&ATurnBox::CharacterOverlapStart);Box->OnComponentEndOverlap.AddDynamic(this, &ATurnBox::CharacterOverlapEnd);
}// Called every frame
void ATurnBox::Tick(float DeltaTime)
{Super::Tick(DeltaTime);}void ATurnBox::CharacterOverlapStart(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{if (ARunGANCharacter* InCharacter =  Cast<ARunGANCharacter>(OtherActor)){InCharacter->bTurn = true;}
}void ATurnBox::CharacterOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{if (ARunGANCharacter* InCharacter = Cast<ARunGANCharacter>(OtherActor)){InCharacter->bTurn = false;}
}

三.开始写路的逻辑,随机生成。

1.准备好道路资源

2.这里将道路,分为直道,转弯道,上下道。使用了UE创建枚举的方式。

#pragma once
#include"CoreMinimal.h"
#include "RunGANType.generated.h"
UENUM()
enum class FRoadType :uint8   //只需要一个字节,更高效 0-255
{StraitFloor,TurnFloor,UPAndDownFloor,MAX,
};

3.然后我们创建道路类,写上通用逻辑。在头文件,将道路类型加上,并前项声明 指针 指向的组件类。

#include "../../RunGANType.h"
#include "RunRoad.generated.h"
class UBoxComponent;
class USceneComponent;
class UStaticMeshComponent;
class UArrowComponent;UCLASS()
class RUNGAN_API ARunRoad : public AActor
{GENERATED_BODY()//场景组件UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "C_J", meta = (AllowPrivateAccess = "true"))USceneComponent* SceneComponent;//根组件UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "C_J", meta = (AllowPrivateAccess = "true"))USceneComponent* RunRoadRootComponent;//碰撞UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "C_J",meta = (AllowPrivateAccess = "true"))UBoxComponent* BoxComponent;//模型UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "C_J",meta = (AllowPrivateAccess = "true"))UStaticMeshComponent* RoadMesh;//方向UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "C_J",meta = (AllowPrivateAccess = "true"))UArrowComponent* SpawnPointMiddle;UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "C_J",meta = (AllowPrivateAccess = "true"))UArrowComponent* SpawnPointRight;UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category = "C_J",meta = (AllowPrivateAccess = "true"))UArrowComponent* SpawnPointLeft;//地板类型UPROPERTY(EditDefaultsOnly,Category = "TowerType")  //EditDefaultsOnly:蓝图可以编译,但是在主编译器不显示所以不可以编译FRoadType RoadType;public:	// Sets default values for this actor's propertiesARunRoad();FTransform GetAttackToTransform(const FVector& MyLocation);
protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:	// Called every framevirtual void Tick(float DeltaTime) override;UFUNCTION()void CharacterOverlapStart(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool  bFromSweep, const FHitResult& SweepResult);// UPrimitiveComponent, OnComponentBeginOverlap, UPrimitiveComponent*, OverlappedComponent, AActor*, OtherActor, UPrimitiveComponent*, OtherComp, int32, OtherBodyIndex, bool, bFromSweep, const FHitResult &, SweepResult);UFUNCTION()void CharacterOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

4.实现逻辑

#include"Components/BoxComponent.h"
#include"Components/SceneComponent.h"
#include"Components/StaticMeshComponent.h"
#include"Components/ArrowComponent.h"

CreateDefaultSubobject<T>实例化组件,SetupAttachment添加组件,Root根组件,Father在根组件下,其余在Father组件下。

ARunRoad::ARunRoad()
{// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.PrimaryActorTick.bCanEverTick = false;//generateBox = CreateDefaultSubobject<UBoxComponent>(TEXT("GenerateBox"));//实例化SceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Father"));RunRoadRootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));RootComponent = RunRoadRootComponent;RoadMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("RoadMesh"));BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("Box"));SpawnPointMiddle = CreateDefaultSubobject<UArrowComponent>(TEXT("SpawnPointMiddle"));SpawnPointRight = CreateDefaultSubobject<UArrowComponent>(TEXT("SpawnPointRight"));SpawnPointLeft = CreateDefaultSubobject<UArrowComponent>(TEXT("SpawnPointLeft"));//附加顺序SceneComponent->SetupAttachment(RootComponent);RoadMesh->SetupAttachment(SceneComponent);BoxComponent->SetupAttachment(SceneComponent);SpawnPointMiddle->SetupAttachment(SceneComponent);SpawnPointRight->SetupAttachment(SceneComponent);SpawnPointLeft->SetupAttachment(SceneComponent);
}

这样初步就将路结构搭建好了。后续开始写GameMode生成每一个路面。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/353588.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

UML详解

1.what is the UML UML 全称是 Unified Modeling Language&#xff08;统一建模语言&#xff09;&#xff0c;它以图形的方式来描述软件的概念 2.它存在的目的 UML 的目标是通过一定结构的表达&#xff0c;来解决现实世界到软件世界的沟通问题。 3.什么是模&#xff0c;…

Centos7安装自动化运维Ansible

自动化运维Devops-Ansible Ansible是新出现的自动化运维工具&#xff0c;基于Python 开发&#xff0c;集合了众多运维工具&#xff08;puppet 、cfengine、chef、func、fabric&#xff09;的优点&#xff0c;实现了批量系统配置 、批量程序部署、批量运行命令 等功能。Ansible…

【每日刷题】Day68

【每日刷题】Day68 &#x1f955;个人主页&#xff1a;开敲&#x1f349; &#x1f525;所属专栏&#xff1a;每日刷题&#x1f34d; &#x1f33c;文章目录&#x1f33c; 1. 451. 根据字符出现频率排序 - 力扣&#xff08;LeetCode&#xff09; 2. 最小的K个数_牛客题霸_牛客…

github连接报本地

一、创建GIthub账号 这里默认大家已经创建好了并且有加速器&#xff0c;能正常上网&#xff0c;然后才能进行下面的操作。 二、创建ssh公钥 网址&#xff1a;Sign in to GitHub GitHub Sign in to GitHub GitHub 进入下面的界面&#xff1a; 然后创建新的密钥 三、官方文…

Excel/WPS《超级处理器》功能介绍与安装下载

超级处理器是基于Excel或WPS开发的一款插件&#xff0c;拥有近300个功能&#xff0c;非常简单高效的处理表格数据&#xff0c;安装即可使用。 点击此处&#xff1a;超i处理器安装下载 Excel菜单&#xff0c;显示如下图所示&#xff1a; WPS菜单显示&#xff0c;如下图所示&am…

【BES2500x系列 -- RTX5操作系统】CMSIS-RTOS RTX -- 实时操作系统的核心,为嵌入式系统注入活力 --(一)

&#x1f48c; 所属专栏&#xff1a;【BES2500x系列】 &#x1f600; 作  者&#xff1a;我是夜阑的狗&#x1f436; &#x1f680; 个人简介&#xff1a;一个正在努力学技术的CV工程师&#xff0c;专注基础和实战分享 &#xff0c;欢迎咨询&#xff01; &#x1f49…

nodejs爬取小红书图片

昨天的文章已经描述了可以抓取评论区内容&#xff0c; 抓取图片内容和抓取评论区的内容基本一致 我们可以看到接口信息中含有图片链接&#xff0c;我们要做的就是爬取图片链接然后下载 这边要用到的模块为const downloadrequire(download) 将爬到的图片链接存放到images数组…

【解决问题】QApplication: No such file or directory,C++ 使用Qt或项目未正确加载Cmake报错

运行环境&#xff1a; Clion编译&#xff0c;构建C工程项目报错QApplication: No such file or directory 问题描述 QApplication: No such file or directory 引用的#include <QApplication>飘红 解决方案 1、Qt没有安装正确&#xff0c;请使用对应版本的Qt。或编译…

各类存储器类型(RAM、ROM、FLASH、DRAM、SRAM)

1 计算机存储类型构成 在计算机中&#xff0c;各类存储器构成了计算机能高速高效运转程序的基石。 计算机的存储体系中&#xff0c;从速度慢到速度快对应着容量大到小&#xff0c;也就是说&#xff0c;速度越快容量越小&#xff1b;容量越大的&#xff0c;速度越慢。两者互相…

Python 数据可视化 多色散点图

Python 数据可视化 多色散点图 fig, ax plt.subplots() max_line max([max(merged_df[unif_ref_value]), max(merged_df[unif_rust_value])]) min_line min([max(merged_df[unif_ref_value]), max(merged_df[unif_rust_value])]) ax.plot([min_line, max_line], [min_line, …

使用 Vue CLI 脚手架生成 Vue 项目

最近我参与了一个前端Vue2的项目。尽管之前也有过参与Vue2项目的经验&#xff0c;但对一些前端Web技术并不十分熟悉。这次在项目中遇到了很多问题&#xff0c;所以我决定借此机会深入学习Vue相关的技术栈。然而&#xff0c;直接开始深入钻研这些技术可能会显得枯燥&#xff0c;…

笔记-python里面的xlrd模块详解

那我就一下面积个问题对xlrd模块进行学习一下&#xff1a; 1.什么是xlrd模块&#xff1f; 2.为什么使用xlrd模块&#xff1f; 3.怎样使用xlrd模块&#xff1f; 1.什么是xlrd模块&#xff1f; ♦python操作excel主要用到xlrd和xlwt这两个库&#xff0c;即xlrd是读excel&…

C#批量设置海康和大华录像机NVR,GB28181的通道编码.

我经常要把小区海康或者大华的硬盘录像机推送到自己搭建的gb28181监控平台,每次几百个摄像头编码,有点头大,就用了1个多周写了个批量设置海康和大华硬盘录像机的通道编码的程序,海康和大华的SDK简直不是人看的. 太乱了. 大华读取通道编码的代码 /// <summary>/// 获取通道…

Pycharm的基础使用

Pycharm的基础使用 一、修改主题 第一步&#xff1a;点击file->settings 第二步&#xff1a;找到Appearance&Behavior->Appearance->Theme选择主题 有五种主题可以选 二、修改默认字体和大小 第一步&#xff1a;打开设置与上面修改主题第一步一样&#xff1b…

Red Hat Ansible Automation Platform架构

目录 示例架构&#xff1a;一、Ansible Automation Platform 实现流程详解1. 自动化控制器 (Automation Controller)2. 自动化网格 (Automation Mesh)3. 私有自动化中心 (Private Automation Hub)4. Event-Driven Ansible 控制器5. 数据存储 (PostgreSQL 数据库) 二、实现流程1…

leetcode498 对角线遍历

题目 给你一个大小为 m x n 的矩阵 mat &#xff0c;请以对角线遍历的顺序&#xff0c;用一个数组返回这个矩阵中的所有元素。 示例 输入&#xff1a;mat [[1,2,3],[4,5,6],[7,8,9]] 输出&#xff1a;[1,2,4,7,5,3,6,8,9] 解析 本题目主要考察的就是模拟法&#xff0c;首…

硬盘格式化NTFS好还是exFAT好 U盘存储文件用哪个格式好? 硬盘用exfat还是ntfs mac不能读取移动硬盘怎么解决

在计算机世界中&#xff0c;文件系统是数据管理的基石&#xff0c;而NTFS和exFAT无疑是这块基石上的两大巨头。它们各自拥有独特的特点和优势&#xff0c;并在不同的使用场景中发挥着重要作用。 什么是文件系统 文件系统提供了组织驱动器的方法。它规定了如何在驱动器上存储数…

外卖APP开发详解:从同城O2O系统源码开始

近期&#xff0c;从事软件开发的小伙伴们都在讨论外卖APP&#xff0c;热度非常之高&#xff0c;所以小编今天将与大家一同探讨同城O2O系统源码、外卖APP开发。 一、外卖APP开发的前期准备 了解目标用户的需求&#xff0c;分析竞争对手的优劣势&#xff0c;明确自身的市场定位。…

揭示西周与汉唐时期的纺织工艺

在中国新疆这片充满神秘色彩的土地上&#xff0c;每一次的考古发掘都仿佛是对历史的一次深情回望&#xff0c;揭示出中华民族悠久而灿烂的文明史。其中&#xff0c;新疆出土的西周和汉唐时期的织物&#xff0c;更是以其精美绝伦的工艺和独特的审美风格&#xff0c;让我们对古代…

Docker部署常见应用之桌面版系统ubuntu-desktop

文章目录 ubuntu-desktop 简介ubuntu-desktop 部署参考文章 ubuntu-desktop 简介 colinchang/ubuntu-desktop 是一个Docker镜像&#xff0c;基于KasmWeb⁠的 Ubuntu 22.04 桌面版&#xff08;Web&#xff09; Docker Image。镜像替换了阿里云Ubuntu Jammy镜像源&#xff0c;安…