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

相关文章

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL