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删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被