UE5 增强输入 自定义Modifier实践

在虚幻旧版输入系统中实现人物的基础移动,运行时同时按住 WS 或 AD ,是会互相抵消,人物停止移动。但在新的增强输入系统中则不会,人物会沿着输入映射情境的 Mappings 数组中两者的后者方向移动。

例如下图,同时按住 WS ,人物会沿着 S 方向移动,因为 S 在 W 之后。

previous_mapping

已知 5.3 版本的输入系统为 InputAction 提供了“累加行为”,所以不再需要为此自定义 Modifier 了。

于是为了实现旧版输入系统的效果,我参考了 Enhanced Input System when pressing both keys for an Axis1D 讨论中 Tezenari 的思路 ,创建了一个自定义修改器 “MutexKey” 来解决此问题。(当然你也可以把移动输入操作 IA_Move 拆成四个单独方向的输入操作来解决)

recent_mapping

running_result

思路

思路就是执行 “MutexKey” 修改器时,如果互斥键位处于按下的状态,那么返回 (0,0,0) 。这样绑定 IA_Move 输入动作的回调函数传入参数的值为 (0,0) ,人物自然不会移动。

注意:两者都需要添加该修改器。如果只有其中一者添加的话,另一方将接管控制权。

蓝图实现

enhanced_input_mutex_key_modifier

blueprintUE

blueprintUE:一个虚幻蓝图分享网站。

如果下方显示不出来的话,可以自行点击 Enhanced Input MutexKey Modifier 打开 blueprintUE 进行复制。

C++实现

InputModifierMutexKey.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once

#include "CoreMinimal.h"
#include "InputModifiers.h"
#include "InputModifierMutexKey.generated.h"

/**
* Mutex Key
* If the mutex key is pressed, returns ZeroVector.
*/
UCLASS(meta = (DisplayName = "MutexKey"))
class TEST_API UInputModifierMutexKey : public UInputModifier
{
GENERATED_BODY()

public:
UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category = Settings, meta = (DisplayName = "Key"))
FKey Key = EKeys::Invalid;

protected:
virtual FInputActionValue ModifyRaw_Implementation(const UEnhancedPlayerInput* PlayerInput, FInputActionValue CurrentValue, float DeltaTime) override;
};
InputModifierMutexKey.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "InputModifierMutexKey.h"
#include "EnhancedPlayerInput.h"

FInputActionValue UInputModifierMutexKey::ModifyRaw_Implementation(const UEnhancedPlayerInput* PlayerInput, FInputActionValue CurrentValue, float DeltaTime)
{
const APlayerController* PC = PlayerInput->GetOuterAPlayerController();

if (!PC)
{
return CurrentValue;
}

FVector Value = CurrentValue.Get<FVector>();
if (Key.IsValid() && PC->IsInputKeyDown(Key))
{
Value = FVector::ZeroVector;
}

return FInputActionValue(CurrentValue.GetValueType(), Value);
}
作者

DullSword

发布于

2023-11-06

更新于

2024-07-02

许可协议

评论