这也是我的第一个想法,但不幸的是您无法获得密钥,您只需将方法绑定到轴名称即可。MoveForward 的“Value”参数保存比例值,但不保存 Key 值。例如,当您按“W”时为 1.0f,当您按“S”时为 -1.0f。
我已经为我的问题找到了解决方案。GetAxisValue(FName) 是用于此目的的错误方法。
相反,我发现 UInputSettings 类包含一个名为 GetAxisMappingByName(FName, TArray) 的方法
这是一个代码片段,它是如何工作的:
// Get the instance of the InputSettings
UInputSettings* InputSettings = UInputSettings::GetInputSettings();
// AxisMappings with all the information will be stored here
TArray<FInputAxisKeyMapping> VerticalKeys;
TArray<FInputAxisKeyMapping> HorizontalKeys;
// Load the AxisMappings
InputSettings->GetAxisMappingByName(FName("MoveForward"), VerticalKeys);
InputSettings->GetAxisMappingByName(FName("MoveRight"), HorizontalKeys);
// Each MovementKey gets its own variable
FKey ForwardKey;
FKey BackKey;
FKey LeftKey;
FKey RightKey;
// Assign each key to the correct direction
for (FInputAxisKeyMapping verticalKey : VerticalKeys)
{
if (verticalKey.Scale == 1.0f)
ForwardKey = verticalKey.Key;
else if (verticalKey.Scale == -1.0f)
BackKey = verticalKey.Key;
}
for (FInputAxisKeyMapping horizontalKey : HorizontalKeys)
{
if (horizontalKey.Scale == 1.0f)
RightKey = horizontalKey.Key;
else if (horizontalKey.Scale == -1.0f)
LeftKey = horizontalKey.Key;
}
分配键是否有比使用 for 循环更好的解决方案?请随意更正我的代码,因为我并不是真正的 C++ 专家。;-)