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

相关文章

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到