创建配置文件
InputMappingContext
InputAction
Character.h
#include "InputActionValue.h"UCLASS()
class DMC_PRATICE_API ADMC_Character_Player : public ADMC_Character_Base
{virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Enhaced Input", meta = (AllowPrivateAccess = "true"))class UInputMappingContext* DefaultMappingContext;//Movevoid MoveInputCallback(const FInputActionValue& Value);void MoveInputCompletedCallback(const FInputActionValue& Value);UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Enhaced Input", meta = (AllowPrivateAccess = "true"))class UInputAction* MoveAction;//Lookvoid LookInputCallback(const FInputActionValue& Value);UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Enhaced Input", meta = (AllowPrivateAccess = "true"))class UInputAction* LookAction;//Jumpvoid JumpPressedCallback(const FInputActionValue& Value);void JumpReleasedCallback(const FInputActionValue& Value);UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Enhaced Input", meta = (AllowPrivateAccess = "true"))class UInputAction* JumpAction;}
Character.cpp
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"void ADMC_Character_Player::BeginPlay()
{Super::BeginPlay();//配置增强输入系统if (APlayerController* L_PlayerController = Cast<APlayerController>(Controller)) {if (UEnhancedInputLocalPlayerSubsystem* SubSystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(L_PlayerController->GetLocalPlayer())) {if (DefaultMappingContext) {SubSystem->AddMappingContext(DefaultMappingContext, 0);}}}
}void ADMC_Character_Player::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{Super::SetupPlayerInputComponent(PlayerInputComponent);if (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent)) {//Lookingif (LookAction) {EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ADMC_Character_Player::LookInputCallback);}//Moveif (MoveAction) {EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ADMC_Character_Player::MoveInputCallback);EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Completed, this, &ADMC_Character_Player::MoveInputCompletedCallback);}//Jumpif (JumpAction) {EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &ADMC_Character_Player::JumpPressedCallback);EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ADMC_Character_Player::JumpReleasedCallback);}
}//Move
void ADMC_Character_Player::MoveInputCallback(const FInputActionValue& Value)
{FVector2D MoveAxisVector = Value.Get<FVector2D>();//TODO : 中断所有攻击的收刀动作//让其在空中移动慢一些float L_SelectScaleValue_Y = GetCharacterMovement()->IsMovingOnGround() ? MoveAxisVector.Y : MoveAxisVector.Y * 0.2f;float L_SelectScaleValue_X = GetCharacterMovement()->IsMovingOnGround() ? MoveAxisVector.X : MoveAxisVector.X * 0.2f;FVector L_ForwardDirection = UKismetMathLibrary::GetForwardVector(FRotator(0.0f, GetControlRotation().Yaw, 0.0f));FVector L_RightDirection = UKismetMathLibrary::GetRightVector(FRotator(0.0f, GetControlRotation().Yaw, 0.0f));AddMovementInput(L_ForwardDirection, L_SelectScaleValue_Y);AddMovementInput(L_RightDirection, L_SelectScaleValue_X);
}void ADMC_Character_Player::MoveInputCompletedCallback(const FInputActionValue& Value)
{FVector2D MoveAxisVector = Value.Get<FVector2D>();
}//Look
void ADMC_Character_Player::LookInputCallback(const FInputActionValue& Value)
{FVector2D LookAxisVector = Value.Get<FVector2D>();AddControllerYawInput(LookAxisVector.X);AddControllerPitchInput(LookAxisVector.Y);}//Jump
void ADMC_Character_Player::JumpPressedCallback(const FInputActionValue& Value)
{UE_LOG(LogTemp, Error, TEXT("JumpPressedCallback"));
}
在蓝图中配置增强输入文件
其他关于 移动的配置
ADMC_Character_Player::ADMC_Character_Player()
{//SpringArm//...SpringArm->bUsePawnControlRotation = true;bUseControllerRotationYaw = false;//Camera//...//Character Movement SettingGetCharacterMovement()->bOrientRotationToMovement = true;}