当前位置: 首页 > article >正文

UE5 第一人称示例代码阅读0 UEnhancedInputComponent

UEnhancedInputComponent使用流程

  • 我的总结
  • 示例分析
    • first
    • then
    • and then
    • finally&代码
      • 关于键盘输入XYZ

我的总结

这个东西是一个对输入进行控制的系统,看了一下第一人称例子里,算是看明白了,但是感觉这东西使用起来有点绕,特此梳理一下
总结来说是这样的 一个context应该对应了一种character,一个context管理几个action,然后就是先要在面板里创建这些东西,最后还需要在代码里去addMappingContext一下以及bindaction一下

示例分析

在这里插入图片描述
首先注意看这里有两个input mapping context

first

也就是你要先创建一个IMC
在这里插入图片描述在这里插入图片描述
点击查看后,各自mapping了几个action,default对应jump move look是控制主人物的
shoot是控制weapon的

then

也就是说你接下来应该创建action,并且在mapping这里绑定好对应的键位和action

and then

这个context能识别key然后映射到action了,接下来就是把context和character绑定好
在这里插入图片描述
找了半天,shoot的绑定在这里

在这里插入图片描述
其他三个比较明显,就在firstperson这
在这里插入图片描述在这里插入图片描述

finally&代码

这里就到了代码绑定阶段了
看头文件FirstPersonCharacter.h
定义那几个action以及对应要执行的函数,这里我看他action的名字和character的input里确实写得一模一样,这里应该哪里有反射啥的吧
另外就是context部分
在这里插入图片描述
在这里插入图片描述

关于键盘输入XYZ

然后这个XY方向啥的就参考第一人称和第三人称的用法好了
类似这个
在这里插入图片描述

// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Logging/LogMacros.h"
#include "FirstPersonCharacter.generated.h"

class UInputComponent;
class USkeletalMeshComponent;
class UCameraComponent;
class UInputAction;
class UInputMappingContext;
struct FInputActionValue;

DECLARE_LOG_CATEGORY_EXTERN(LogTemplateCharacter, Log, All);

UCLASS(config=Game)
class AFirstPersonCharacter : public ACharacter
{
	GENERATED_BODY()

	/** Pawn mesh: 1st person view (arms; seen only by self) */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Mesh, meta = (AllowPrivateAccess = "true"))
	USkeletalMeshComponent* Mesh1P;

	/** First person camera */
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
	UCameraComponent* FirstPersonCameraComponent;

	/** Jump Input Action */
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true"))
	UInputAction* JumpAction;

	/** Move Input Action */
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=Input, meta=(AllowPrivateAccess = "true"))
	UInputAction* MoveAction;
	
public:
	AFirstPersonCharacter();

protected:
	virtual void BeginPlay();

public:
		
	/** Look Input Action */
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
	class UInputAction* LookAction;

protected:
	/** Called for movement input */
	void Move(const FInputActionValue& Value);

	/** Called for looking input */
	void Look(const FInputActionValue& Value);

protected:
	// APawn interface
	virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override;
	// End of APawn interface

public:
	/** Returns Mesh1P subobject **/
	USkeletalMeshComponent* GetMesh1P() const { return Mesh1P; }
	/** Returns FirstPersonCameraComponent subobject **/
	UCameraComponent* GetFirstPersonCameraComponent() const { return FirstPersonCameraComponent; }

};


然后是实现
主要就是调用 EnhancedInputComponent->BindAction这个把对应函数绑定上去就好了

// Copyright Epic Games, Inc. All Rights Reserved.

#include "FirstPersonCharacter.h"
#include "FirstPersonProjectile.h"
#include "Animation/AnimInstance.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "InputActionValue.h"
#include "Engine/LocalPlayer.h"

DEFINE_LOG_CATEGORY(LogTemplateCharacter);

//
// AFirstPersonCharacter

AFirstPersonCharacter::AFirstPersonCharacter()
{
	// Set size for collision capsule
	GetCapsuleComponent()->InitCapsuleSize(55.f, 96.0f);
		
	// Create a CameraComponent	
	FirstPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
	FirstPersonCameraComponent->SetupAttachment(GetCapsuleComponent());
	FirstPersonCameraComponent->SetRelativeLocation(FVector(-10.f, 0.f, 60.f)); // Position the camera
	FirstPersonCameraComponent->bUsePawnControlRotation = true;

	// Create a mesh component that will be used when being viewed from a '1st person' view (when controlling this pawn)
	Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("CharacterMesh1P"));
	Mesh1P->SetOnlyOwnerSee(true);
	Mesh1P->SetupAttachment(FirstPersonCameraComponent);
	Mesh1P->bCastDynamicShadow = false;
	Mesh1P->CastShadow = false;
	//Mesh1P->SetRelativeRotation(FRotator(0.9f, -19.19f, 5.2f));
	Mesh1P->SetRelativeLocation(FVector(-30.f, 0.f, -150.f));

}

void AFirstPersonCharacter::BeginPlay()
{
	// Call the base class  
	Super::BeginPlay();
}

 Input

void AFirstPersonCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{	
	// Set up action bindings
	if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent))
	{
		// Jumping
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &ACharacter::Jump);
		EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);

		// Moving
		EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AFirstPersonCharacter::Move);

		// Looking
		EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &AFirstPersonCharacter::Look);
	}
	else
	{
		UE_LOG(LogTemplateCharacter, Error, TEXT("'%s' Failed to find an Enhanced Input Component! This template is built to use the Enhanced Input system. If you intend to use the legacy system, then you will need to update this C++ file."), *GetNameSafe(this));
	}
}


void AFirstPersonCharacter::Move(const FInputActionValue& Value)
{
	// input is a Vector2D
	FVector2D MovementVector = Value.Get<FVector2D>();

	if (Controller != nullptr)
	{
		// add movement 
		AddMovementInput(GetActorForwardVector(), MovementVector.Y);
		AddMovementInput(GetActorRightVector(), MovementVector.X);
	}
}

void AFirstPersonCharacter::Look(const FInputActionValue& Value)
{
	// input is a Vector2D
	FVector2D LookAxisVector = Value.Get<FVector2D>();

	if (Controller != nullptr)
	{
		// add yaw and pitch input to controller
		AddControllerYawInput(LookAxisVector.X);
		AddControllerPitchInput(LookAxisVector.Y);
	}
}

http://www.kler.cn/news/365769.html

相关文章:

  • 【C++篇】栈的层叠与队列的流动:在 STL 的韵律中探寻数据结构的优雅之舞
  • FFMPEG+Qt 实时显示本机USB摄像头1080p画面以及同步录制mp4视频
  • transforms的使用
  • springboot3.x.x 集成 连接SQL Server 2008 驱动版本和SSL套接字问题的解决
  • Angular 保姆级别教程高阶应用 - RxJs
  • Discuz 论坛开发一套传奇发布站与传奇开服表
  • python实现斗地主
  • qt项目使用其他项目的.ui之指针
  • RabbitMQ安装部署
  • Elliott Wave Prophet,艾略特波浪预测指标!预测未来走势!免费公式!(指标教程)
  • 考研篇——数据结构王道3.2.2_队列的顺序实现
  • 大一物联网要不要转专业,转不了该怎么办?
  • Scala的多态:定义,作用,实现手法
  • AUTOSAR从入门到精通-英飞凌GTM模块
  • Go语言生成UUID的利器:github.com/google/uuid
  • Node.js 路由
  • 文本预处理——构建词云
  • 【云效】阿里云云效:一站式DevOps平台介绍与使用教程(图文)附PPT
  • 2024 项目管理工具大变革:Jira 的替代者是谁?
  • 【数据分享】全国各省份农业-瓜果类面积(1993-2018年)
  • Python+Django+VUE 搭建深度学习训练界面 (持续ing)
  • CRLF、UTF-8这些编辑器右下角的选项的意思
  • STM32Lx GXHT3x SHT3x iic 驱动开发应用详解
  • 【Git】将本地代码提交到github仓库
  • 【Unity 安装教程】
  • Node.js 进阶:V8 垃圾回收机制全解析