unreal-enhanced-input
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUnreal Enhanced Input
Unreal Enhanced Input
Wire player input the modern UE5 way with the Enhanced Input system: data-driven Input
Actions and Mapping Contexts instead of the legacy Project Settings axis/action mappings.
Targets UE 5.4+ (Enhanced Input is the default; legacy input is deprecated).
使用Enhanced Input系统以UE5的现代方式配置玩家输入:采用数据驱动的Input Actions和Mapping Contexts,替代传统的项目设置轴/动作映射。适用于**UE 5.4+**版本(Enhanced Input为默认选项;传统输入已被弃用)。
When to use
使用场景
- Use when adding movement/look/jump/fire input, creating Input Action () and Input Mapping Context (
IA_) assets, applying modifiers/triggers, adding a mapping context to a player, or binding actions in C++ or Blueprints.IMC_ - Use when the project has /
IA_*assets or referencesIMC_*.EnhancedInput
When not to use: engine-agnostic input architecture (rebinding strategy, buffering,
multi-device design) → . The Pawn/Character C++ those bindings live in →
.
input-systemsunreal-cpp-gameplay- 适用于添加移动/视角/跳跃/射击输入、创建Input Action()和Input Mapping Context(
IA_)资源、应用修改器/触发器、为玩家添加映射上下文,或在C++或Blueprints中绑定动作的场景。IMC_ - 适用于项目包含/
IA_*资源或引用IMC_*的情况。EnhancedInput
不适用于: 引擎无关的输入架构(重绑定策略、缓冲、多设备设计)→ 请参考。绑定所在的Pawn/Character C++类 → 请参考。
input-systemsunreal-cpp-gameplayCore workflow
核心工作流程
- Enable the module/plugin. Enhanced Input is on by default in UE5; for C++ binding add
to
"EnhancedInput"inPublicDependencyModuleNames.*.Build.cs - Create Input Actions (). Each has a Value Type:
IA_for buttons,Digital (bool)for triggers,Axis1D (float)for movement/look.Axis2D (Vector2D) - Create an Input Mapping Context () that maps keys/buttons to those actions. Use Modifiers to shape raw input (Negate, Swizzle Input Axis Values, Dead Zone) — e.g. WASD into one Axis2D needs Negate on A/S and a Swizzle on W/S. Use Triggers (Pressed, Hold, Tap) to decide when an action fires.
IMC_ - Add the mapping context to the player via the (
EnhancedInputLocalPlayerSubsystem), usually inAddMappingContext(IMC, Priority)/possession.BeginPlay - Bind actions to handlers by (
ETriggerEvent,Triggered,Started, …) on theCompleted, and read theEnhancedInputComponentin the handler.FInputActionValue - Verify in PIE; the Enhanced Input debugging console commands
() show which actions trigger and their values.
showdebug enhancedinput
- 启用模块/插件。Enhanced Input在UE5中默认开启;若要在C++中绑定,需在的
*.Build.cs中添加PublicDependencyModuleNames。"EnhancedInput" - 创建Input Actions()。每个动作都有一个值类型:
IA_适用于按钮,Digital (bool)适用于扳机键,Axis1D (float)适用于移动/视角控制。Axis2D (Vector2D) - 创建Input Mapping Context(),将按键/按钮映射到这些动作。使用修改器调整原始输入(取反、交换输入轴值、死区)——例如,将WASD映射到单个Axis2D时,需要对A/S键取反,对W/S键交换输入轴值。使用触发器(按下、按住、点击)决定动作触发的时机。
IMC_ - 通过为玩家添加映射上下文(
EnhancedInputLocalPlayerSubsystem),通常在AddMappingContext(IMC, Priority)或角色拥有时执行。BeginPlay - 在上通过
EnhancedInputComponent(ETriggerEvent、Triggered、Started等)将动作绑定到处理函数,并在处理函数中读取Completed。FInputActionValue - 在PIE(Play In Editor)中验证;Enhanced Input调试控制台命令()可显示触发的动作及其数值。
showdebug enhancedinput
Patterns
实现模式
1. Add the mapping context (C++ Character)
1. 添加映射上下文(C++ Character类)
cpp
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
if (APlayerController* PC = Cast<APlayerController>(GetController()))
if (ULocalPlayer* LP = PC->GetLocalPlayer())
if (auto* Subsystem = LP->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>())
Subsystem->AddMappingContext(DefaultMappingContext, /*Priority*/ 0);
}
// DefaultMappingContext is a UPROPERTY(EditAnywhere) TObjectPtr<UInputMappingContext>.cpp
void AMyCharacter::BeginPlay()
{
Super::BeginPlay();
if (APlayerController* PC = Cast<APlayerController>(GetController()))
if (ULocalPlayer* LP = PC->GetLocalPlayer())
if (auto* Subsystem = LP->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>())
Subsystem->AddMappingContext(DefaultMappingContext, /*Priority*/ 0);
}
// DefaultMappingContext是UPROPERTY(EditAnywhere) TObjectPtr<UInputMappingContext>类型。2. Bind actions and read values
2. 绑定动作并读取数值
cpp
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
// The component is an Enhanced Input component when the plugin is active.
if (UEnhancedInputComponent* EIC = Cast<UEnhancedInputComponent>(InputComponent))
{
EIC->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyCharacter::Move);
EIC->BindAction(LookAction, ETriggerEvent::Triggered, this, &AMyCharacter::Look);
EIC->BindAction(JumpAction, ETriggerEvent::Started, this, &ACharacter::Jump);
EIC->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
}
}
void AMyCharacter::Move(const FInputActionValue& Value)
{
const FVector2D Axis = Value.Get<FVector2D>(); // Axis2D action
AddMovementInput(GetActorForwardVector(), Axis.Y);
AddMovementInput(GetActorRightVector(), Axis.X);
}cpp
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
// 当插件激活时,该组件为Enhanced Input组件。
if (UEnhancedInputComponent* EIC = Cast<UEnhancedInputComponent>(InputComponent))
{
EIC->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyCharacter::Move);
EIC->BindAction(LookAction, ETriggerEvent::Triggered, this, &AMyCharacter::Look);
EIC->BindAction(JumpAction, ETriggerEvent::Started, this, &ACharacter::Jump);
EIC->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
}
}
void AMyCharacter::Move(const FInputActionValue& Value)
{
const FVector2D Axis = Value.Get<FVector2D>(); // Axis2D类型的动作
AddMovementInput(GetActorForwardVector(), Axis.Y);
AddMovementInput(GetActorRightVector(), Axis.X);
}3. Blueprint equivalent (node flow)
3. 蓝图等效实现(节点流程)
text
Event BeginPlay
-> Get Controller -> Cast To PlayerController -> Get Local Player
-> Get EnhancedInputLocalPlayerSubsystem -> Add Mapping Context (IMC_Default, Priority 0)
// IA_Move is exposed as its own event node in the Character's Event Graph:
EnhancedInputAction IA_Move (Triggered)
-> Action Value (Vector2D) -> Add Movement Input (Forward * Y, Right * X)text
Event BeginPlay
-> 获取控制器 -> 转换为PlayerController -> 获取本地玩家
-> 获取EnhancedInputLocalPlayerSubsystem -> 添加映射上下文(IMC_Default,优先级0)
// IA_Move在Character的事件图表中作为独立事件节点暴露:
EnhancedInputAction IA_Move (Triggered)
-> 动作数值(Vector2D) -> 添加移动输入(向前向量 * Y,向右向量 * X)Pitfalls
常见陷阱
- No input at all — the mapping context was never added (), or the player has no Local Player yet. Add it after possession/
AddMappingContext.BeginPlay - Link/compile error binding in C++ — isn't in
"EnhancedInput"Build.cs.PublicDependencyModuleNames - WASD only moves on two keys / wrong axis — Axis2D needs Modifiers: Negate on the negative keys (A, S) and a Swizzle Input Axis Values on the vertical (W/S) so both axes map correctly. Raw bindings without modifiers misbehave.
- returns zero — value-type mismatch: the Input Action is Digital/Axis1D, not Axis2D. Match the
Get<FVector2D>()to the action's Value Type.Get<T>() - Action fires every frame unexpectedly — repeats while held for a Down trigger; use
Triggered/Startedfor one-shots (jump press/release), or a Pressed/Tap trigger.Completed - Two contexts fight — multiple mapping contexts stack by Priority; a higher-priority
context can consume a key. Manage with priorities and .
RemoveMappingContext
- 完全无输入——映射上下文从未添加(未调用),或者玩家尚未拥有本地玩家。应在角色拥有后或
AddMappingContext时添加。BeginPlay - C++绑定时出现链接/编译错误——的
Build.cs中未添加PublicDependencyModuleNames。"EnhancedInput" - WASD仅能通过两个按键移动/轴方向错误——Axis2D需要修改器:对负方向按键(A、S)取反,对垂直方向(W/S)交换输入轴值,这样两个轴才能正确映射。未使用修改器的原始绑定会出现异常行为。
- 返回零值——值类型不匹配:Input Action是Digital/Axis1D类型,而非Axis2D。需确保
Get<FVector2D>()与动作的值类型匹配。Get<T>() - 动作意外每帧触发——对于按下触发器,会在按住期间重复触发;对于一次性操作(跳跃按下/释放),请使用
Triggered/Started,或使用Pressed/Tap触发器。Completed - 两个上下文冲突——多个映射上下文按优先级堆叠;高优先级上下文会占用按键。需通过优先级管理和来处理。
RemoveMappingContext
References
参考资料
- For a complete first-/third-person C++ Character with Enhanced Input (header + source,
including look/jump and a control-rebind note), read .
references/cpp-setup.md - Primary docs: "Enhanced Input in Unreal Engine"
().
https://dev.epicgames.com/documentation/en-us/unreal-engine/enhanced-input-in-unreal-engine
- 如需完整的第一/第三人称C++ Character类(包含Enhanced Input的头文件和源文件,包括视角/跳跃控制及重绑定说明),请阅读。
references/cpp-setup.md - 官方主文档:"Enhanced Input in Unreal Engine"()。
https://dev.epicgames.com/documentation/en-us/unreal-engine/enhanced-input-in-unreal-engine
Related skills
相关技能
- — engine-agnostic input architecture and rebinding strategy.
input-systems - — the Character/Pawn class and module setup.
unreal-cpp-gameplay - — composes input with a 3D controller and shooting.
fps-shooter
- ——引擎无关的输入架构和重绑定策略。
input-systems - ——Character/Pawn类及模块设置。
unreal-cpp-gameplay - ——将输入与3D控制器和射击系统结合。",
fps-shooter