UE4学习笔记13th:使用C++ 代码以对输入进行响应

2023-12-02 01:10

本文主要是介绍UE4学习笔记13th:使用C++ 代码以对输入进行响应,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这节对输入进行响应。
首先打开PawnWithCamera.h添加定义:

 //输入变量
FVector2D MovementInput;
FVector2D CameraInput;
float ZoomFactor;
bool bZoomingIn;

紧接着创建函数来追溯输入:

//输入函数
void MoveForward(float AxisValue);
void MoveRinht(float AxisValue);
void PitchCamera(float AxisValue);
void YawCamera(float AxisValue);
void ZoomIn();
void ZoomOut();

这里写图片描述

在PawnWithCamera.cpp中添加代码:

//处理输入
void APawnWithCamera::MoveForward(float AxisValue)
{
MovementInput.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}void APawnWithCamera::MoveRight(float AxisValue)
{
MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}void APawnWithCamera::PitchCamera(float AxisValue)
{
CameraInput.Y = AxisValue;
}void APawnWithCamera::YawCamera(float AxisValue)
{
CameraInput.X = AxisValue;
}void APawnWithCamera::ZoomIn()
{
bZoomingIn = true;
}void APawnWithCamera::ZoomOut()
{
bZoomingIn = false;
}

这里写图片描述

绑定到ApawnWithCamera::SetupPlayerInputComponent

//绑定到事件:“ZoomIn”
InputComponent->BindAction("ZoomIn", IE_Pressed, this, &APawnWithCamera::ZoomIn);
InputComponent->BindAction("ZoomIn", IE_Released, this, &APawnWithCamera::ZoomOut);////为四条轴绑定对每帧的处理
InputComponent->BindAxis("MoveForward", this, &APawnWithCamera::MoveForward);
InputComponent->BindAxis("MoveRight", this, &APawnWithCamera::MoveRight);
InputComponent->BindAxis("CameraYaw", this, &APawnWithCamera::YawCamera);
InputComponent->BindAxis("CameraPitch", this, &APawnWithCamera::PitchCamera);//

这里写图片描述

在APawnWithCamera::Tick中添加:用于更新每一帧的Pawn和Camera

//按下放大,否则缩小
{
if (bZoomingIn)
{
ZoomFactor += DeltaTime / 0.5f;//放大一半
}
else
{
ZoomFactor -= DeltaTime / 0.25f;//缩小四分之一
}
ZoomFactor = FMath::Clamp<float>(ZoomFactor, 0.0f, 1.0f);
//基于ZoomFActor来混合相机的视域和弹簧臂的长度
OurCamera->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomFactor);
OurCameraSpringArm->TargetArmLength = FMath::Lerp<float>(400.0f, 300.0f, ZoomFactor);
}

这里写图片描述

这个代码段会直接使用鼠标的X轴来旋转 Pawn 的偏转,但只有相机会对Y轴的变更进行响应, 旋转任意 Actor或 Actor 子类实际上会旋转根级Component ,而这又会间接影响所有附着的内容。

//旋转Actor的偏转,同时旋转附着在Actor上的相机
{
FRotator NewRotation = GetActorRotation();
NewRotation.Yaw += CameraInput.X;
SetActorRotation(NewRotation);
}////旋转相机的倾斜,使总是向下看
{
FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);
OurCameraSpringArm->SetWorldRotation(NewRotation);
}

这里写图片描述
处理移动:

//基于“MoveX”和“MoveY”坐标轴来处理移动
{
if (!MovementInput.IsZero())
{
//把移动输入坐标轴的值每秒缩放100个单位
MovementInput = MovementInput.SafeNormal() * 100.0f;//
FVector NewLocation = GetActorLocation();
NewLocation += GetActorForwardVector() * MovementInput.X *DeltaTime;
NewLocation += GetActorRightVector() * MovementInput.Y *DeltaTime;
SetActorLocation(NewLocation);
}
}

这里写图片描述

拖曳新类的一个实例到编辑器中的关卡编辑器窗口
这里写图片描述

按下Play,这是鼠标点击后的效果,你可以使用WASD控制移动,使用鼠标控制方向和缩放。

这里写图片描述

完整代码:

PawnWithCamera.h
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "GameFramework/Pawn.h"
#include "PawnWithCamera.generated.h"UCLASS()
class HOWTO_PLAYERCAMERA_API APawnWithCamera : public APawn
{
GENERATED_BODY()public:
// Sets default values for this pawn's properties
APawnWithCamera();// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;protected:
UPROPERTY(EditAnywhere)
USpringArmComponent* OurCameraSpringArm;
UCameraComponent* OurCamera;
//输入变量
FVector2D MovementInput;
FVector2D CameraInput;
float ZoomFactor;
bool bZoomingIn;////输入函数
void MoveForward(float AxisValue);
void MoveRight(float AxisValue);
void PitchCamera(float AxisValue);
void YawCamera(float AxisValue);
void ZoomIn();
void ZoomOut();//
};PawnWithCamera.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include "HowTo_PlayerCamera.h"
#include "PawnWithCamera.h"// Sets default values
APawnWithCamera::APawnWithCamera()
{// 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;//创建组件
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
OurCameraSpringArm->AttachTo(RootComponent);
OurCameraSpringArm->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 50.0f), FRotator(-60.0f, 0.0f, 0.0f));
OurCameraSpringArm->TargetArmLength = 400.0f;
OurCameraSpringArm->bEnableCameraLag = true;
OurCameraSpringArm->CameraLagSpeed = 3.0f;////创建摄像机并附加到SpringArm组件上
OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));
OurCamera->AttachTo(OurCameraSpringArm, USpringArmComponent::SocketName);////控制默认玩家
AutoPossessPlayer = EAutoReceiveInput::Player0;//}// Called when the game starts or when spawned
void APawnWithCamera::BeginPlay()
{
Super::BeginPlay();
}// Called every frame
void APawnWithCamera::Tick( float DeltaTime )
{
Super::Tick(DeltaTime);//按下放大,否则缩小
{
if (bZoomingIn)
{
ZoomFactor += DeltaTime / 0.5f;//放大一半
}
else
{
ZoomFactor -= DeltaTime / 0.25f;//缩小四分之一
}
ZoomFactor = FMath::Clamp<float>(ZoomFactor, 0.0f, 1.0f);
//基于ZoomFActor来混合相机的视域和弹簧臂的长度
OurCamera->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomFactor);
OurCameraSpringArm->TargetArmLength = FMath::Lerp<float>(400.0f, 300.0f, ZoomFactor);//
}////旋转Actor的偏转,同时旋转附着在Actor上的相机
{
FRotator NewRotation = GetActorRotation();
NewRotation.Yaw += CameraInput.X;
SetActorRotation(NewRotation);
}////旋转相机的倾斜,使总是向下看
{
FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);
OurCameraSpringArm->SetWorldRotation(NewRotation);
}////基于“MoveX”和“MoveY”坐标轴来处理移动
{
if (!MovementInput.IsZero())
{
//把移动输入坐标轴的值每秒缩放100个单位
MovementInput = MovementInput.SafeNormal() * 100.0f;//
FVector NewLocation = GetActorLocation();
NewLocation += GetActorForwardVector() * MovementInput.X *DeltaTime;
NewLocation += GetActorRightVector() * MovementInput.Y *DeltaTime;
SetActorLocation(NewLocation);
}
}//
}// Called to bind functionality to input
void APawnWithCamera::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);//绑定到事件:“ZoomIn”
InputComponent->BindAction("ZoomIn", IE_Pressed, this, &APawnWithCamera::ZoomIn);
InputComponent->BindAction("ZoomIn", IE_Released, this, &APawnWithCamera::ZoomOut);////为四条轴绑定对每帧的处理
InputComponent->BindAxis("MoveForward", this, &APawnWithCamera::MoveForward);
InputComponent->BindAxis("MoveRight", this, &APawnWithCamera::MoveRight);
InputComponent->BindAxis("CameraYaw", this, &APawnWithCamera::YawCamera);
InputComponent->BindAxis("CameraPitch", this, &APawnWithCamera::PitchCamera);//}//处理输入
void APawnWithCamera::MoveForward(float AxisValue)
{
MovementInput.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}void APawnWithCamera::MoveRight(float AxisValue)
{
MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
}void APawnWithCamera::PitchCamera(float AxisValue)
{
CameraInput.Y = AxisValue;
}void APawnWithCamera::YawCamera(float AxisValue)
{
CameraInput.X = AxisValue;
}void APawnWithCamera::ZoomIn()
{
bZoomingIn = true;
}void APawnWithCamera::ZoomOut()
{
bZoomingIn = false;
}//

这篇关于UE4学习笔记13th:使用C++ 代码以对输入进行响应的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/443393

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部