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

news/2024/10/26 21:42:55/

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 interfacevirtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override;// End of APawn interfacepublic:/** 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);//
// AFirstPersonCharacterAFirstPersonCharacter::AFirstPersonCharacter()
{// Set size for collision capsuleGetCapsuleComponent()->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 cameraFirstPersonCameraComponent->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();
} Inputvoid AFirstPersonCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{	// Set up action bindingsif (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent)){// JumpingEnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &ACharacter::Jump);EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);// MovingEnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AFirstPersonCharacter::Move);// LookingEnhancedInputComponent->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 Vector2DFVector2D 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 Vector2DFVector2D LookAxisVector = Value.Get<FVector2D>();if (Controller != nullptr){// add yaw and pitch input to controllerAddControllerYawInput(LookAxisVector.X);AddControllerPitchInput(LookAxisVector.Y);}
}

http://www.ppmy.cn/news/1542208.html

相关文章

【Linux学习】(3)Linux的基本指令操作

前言 配置Xshell登录远程服务器Linux的基本指令——man、cp、mv、alias&which、cat&more&less、head&tail、date、cal、find、grep、zip&tar、bc、unameLinux常用热键 一、配置Xshell登录远程服务器 以前我们登录使用指令&#xff1a; ssh 用户名你的公网…

如何使用postman进行自动化

接口测试是确保系统稳定运行的关键环节&#xff0c;而Postman作为一款常用的接口测试工具&#xff0c;以其直观易用的界面和强大的功能&#xff0c;极大地简化了接口测试的过程。 一、准备工作 准备接口相关资料&#xff1a;包括接口文档、请求方法、URL、请求头、请求体等信…

监控场景下,视频SDK的应用策略

在当今数字化、智能化的时代背景下&#xff0c;音视频技术的快速发展正深刻改变着各行各业。特别是在监控领域&#xff0c;音视频SDK的应用不仅极大地提升了监控系统的性能与效率&#xff0c;还推动了监控技术的智能化转型。 一、音视频SDK 音视频SDK是一套集成了音视频编解码…

pnpm : 无法加载文件...

1.全局安装pnpm npm i pnpm -g 2.以管理员身份打开Windows PowerShell&#xff0c;执行命令 Set-ExecutionPolicy RemoteSigned -Scope CurrentUser 3.输入 Y ,回车 pnpm -v 出现版本信息 即为成功

Clickhouse集群_Zookeeper配置的dataDir目录磁盘占有率接近100%时,该dataDir目录是否可以清理及如何清理的脚本

官方文档https://zookeeper.apache.org/doc/r3.1.2/zookeeperAdmin.html#OngoingDataDirectoryCleanup 监控报警发现clickhouse集群环境的数据库节点磁盘报警&#xff0c;检查下来发现/chdata/zookeeper/data/version-2/目录特别大&#xff0c;里面包含了log.*文件和snapshot.…

在flask微服务中使用调度器设置定时任务:BackgroundScheduler

在flask微服务中使用调度器设置定时任务&#xff1a;BackgroundScheduler #!/usr/bin/env python3 # -*- coding: UTF-8 -*- # filename: main.pyimport os import unittestfrom app import create_app, db from app.route import route from app.service.score_service impor…

spring源码拓展点3之addBeanPostProcesser

概述 在refresh方法中的prepareBeanFactory方法中&#xff0c;有一个拓展点&#xff1a;addBeanPostProcessor。即通过注入Aware对象从而将容器中的某些值设置到某个bean中。 beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));aware接口调用 …

Linux服务器自动化批量安装IB网卡驱动

Readme 脚本功能&#xff1a; 自动化批量安装IB网卡驱动 使用方法&#xff1a; 将该脚本上传至linux服务器内将驱动包上传至与脚本同一级目录下在脚本同一级目录下创建nodelist文件&#xff0c;写入待安装节点的带内管理IP&#xff0c;每行一个&#xff1b;脚本赋执行权限 …