参考视频:https://www.bilibili.com/video/BV1Tr4y1b7C6
参考文章:https://zhuanlan.zhihu.com/p/522171880
https://docs.unrealengine.com/5.1/zh-CN/enhanced-input-in-unreal-engine/
UE5.3中已经将之前的轴和操作映射废弃掉了,默认使用增强输入(Enhanced Input)。
之后再补充。
1、先在内容管理器中右键-输入-输入操作(Input Action)和输入映射情景(Input Mapping Context),这里创建了五个输入操作和两个输入映射情景。
2、双击打开创建的输入操作,输入操作有四种值类型可选,默认是数字(布尔),这里将Forward_Backward和Left_Right两个输入操作设置成了Axis1D(浮点)。
3、双击打开创建的输入映射情景,这里打开MoveBase,点击映射旁的加号添加新项,选择想要绑定映射的输入操作资产,然后点击旁的加号添加新项,然后选择想要绑定的键位,如果想要负值,则点击键位下的修改器旁的加号添加新项,并选择否定(Negate)(相当于老式轴映射中的给缩放设置-1.0),这里主要就添加了Forward_Backward和Left_Right两个输入操作,实现WSAD操作。
4、新建一个继承自AGameModeBase的类和一个APawn的类。
其中GeometrySandboxGameModeBase不需要做任何修改,主要是SandboxPawn。
首先添加一些成员,以及两个处理函数。
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ACTION")
TObjectPtr<UInputMappingContext> IMC_ACTION;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ACTION")
TObjectPtr<UInputMappingContext> IMC_MOVEBASE;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ACTION")
TObjectPtr<UInputAction> IA_MoveForwardBackward;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ACTION")
TObjectPtr<UInputAction> IA_MoveLeftRight;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ACTION")
TObjectPtr<UInputAction> IA_TurnLeftRight;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ACTION")
TObjectPtr<UInputAction> IA_LookUpDown;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "ACTION")
TObjectPtr<UInputAction> IA_Jump;
void MoveForward(const FInputActionValue& inputValue);
void MoveRight(const FInputActionValue& inputValue);
void ASandboxPawn::MoveForward(const FInputActionValue& inputValue)
{
float Amount = inputValue.GetMagnitude();
UE_LOG(LogSandboxPawn, Display, TEXT("MoveForward run ! Value : %f"), Amount);
VelocityVector.X = Amount;
}
void ASandboxPawn::MoveRight(const FInputActionValue& inputValue)
{
float Amount = inputValue.GetMagnitude();
UE_LOG(LogSandboxPawn, Display, TEXT("MoveRight run ! Value : %f"), Amount);
VelocityVector.Y = Amount;
}
然后在SetupPlayerInputComponent函数中添加绑定。这里注意,ETriggerEvent具有已开始(Started)、进行中(Ongoing)、已触发(Triggered)、已完成(Completed)和已取消(Canceled)这几种状态,具体请见参考文章【1】,这里主要是Triggered和Completed,当按键按下时会触发Triggered,参数值为1,弹起以Completed结束,参数值为0,只绑定Triggered时,当按键弹起,VelocityVector没有赋0,则依旧会朝原方向移动,所以两个都需要绑定。
auto controller = Cast<APlayerController>(GetController());
if (controller != nullptr)
{
UE_LOG(LogSandboxPawn, Warning, TEXT("PlayerController available !"));
auto subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(controller->GetLocalPlayer());
if (subsystem != nullptr)
{
UE_LOG(LogSandboxPawn, Warning, TEXT("EnhancedInputLocalPlayerSubsystem available !"));
subsystem->AddMappingContext(IMC_ACTION, 0);
subsystem->AddMappingContext(IMC_MOVEBASE, 0);
}
}
auto enhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent);
if (enhancedInputComponent != nullptr)
{
UE_LOG(LogSandboxPawn, Warning, TEXT("EnhancedInputComponent available !"));
if (!IA_MoveForwardBackward.IsNull())
{
enhancedInputComponent->BindAction(IA_MoveForwardBackward, ETriggerEvent::Triggered, this, &ASandboxPawn::MoveForward);
enhancedInputComponent->BindAction(IA_MoveForwardBackward, ETriggerEvent::Completed, this, &ASandboxPawn::MoveForward);
}
if (!IA_MoveLeftRight.IsNull())
{
enhancedInputComponent->BindAction(IA_MoveLeftRight, ETriggerEvent::Triggered, this, &ASandboxPawn::MoveRight);
enhancedInputComponent->BindAction(IA_MoveLeftRight, ETriggerEvent::Completed, this, &ASandboxPawn::MoveRight);
}
}
最后在Tick函数中添加变换。
if (!VelocityVector.IsZero())
{
const FVector NewLocation = GetActorLocation() + Velocity * DeltaTime * VelocityVector;
SetActorLocation(NewLocation);
}
5、编译,然后进入编辑器,将两个类右键创建蓝图类。
双击打开BP_GeometrySandboxGameModeBase,将细节中的默认Pawn类设置为BP_SandboxPawn。
双击打开BP_SandboxPawn,给细节中ACTION下的参数选择对应的资产。
最后将世界场景设置中的游戏模式重载设置成BP_GeometrySandboxGameModeBase。
运行场景,按下W,场景摄像机向前移动,查看Log,会发现MoveForward函数被调用,并且值持续为1,当W弹起后,值为0。