创建一个第三人称蓝图模板,开启和配置 Enhanced Input 插件如下
然后在项目设置里修改默认输入系统
编辑 IA_Jump如下
ValueType选择Digital,跳跃不需要具体值,只需要是否就可以表达。
编辑IMC_Action如下
在第三人称蓝图里绑定跳跃操作。(注意要删除工程自带的IA_Jump,绑定自己创建的。后面的打印可以不需要。)
编译保存运行,可以看到角色可以跳跃了。(打印的是ValueType)
编辑IA_MoveForward_Backword,把ValueType改成Axis2D,移动需要具体值,值越高速移动度越快。
编辑IMC_Action,加入按键绑定
这里S要为负值,所以要在Modifiers里选Negate。
最后编辑角色蓝图
保存编译运行,角色可以前后移动了
新建2个Input Action
进入编辑,这里都选择Axis2D
然后在IMC_Action里绑定按键操作
在角色蓝图里绑定如下:
保存编译运行,视角可以上下左右看了。
在UE5.1里,第三人称模板默认启用了Enhanced Input,可以看看官方是如何使用的。
操作和蓝图一样
编辑 EIHCPP.Build.cs,加入 EnhancedInput 引用,代码如下:
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
public class EIHCPP : ModuleRules
{
public EIHCPP(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput" });
PrivateDependencyModuleNames.AddRange(new string[] { });
}
}
3个输入操作蓝图名称分别为IA_Jump, IA_Look和IA_Move
分别编辑如下
IHCPPCharacter.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "InputActionValue.h"
#include "IHCPPCharacter.generated.h"
UCLASS()
class EIHCPP_API AIHCPPCharacter : public ACharacter
{
GENERATED_BODY()
public:
AIHCPPCharacter();
// 弹性臂组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom;
// 摄像机组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera;
// 输入映射情境
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputMappingContext* DefaultMappingContext;
// 角色跳输入操作
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* JumpAction;
// 角色移动输入操作
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* MoveAction;
// 视角输入操作
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* LookAction;
protected:
virtual void BeginPlay() override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// 移动操作回调方法
void Move(const FInputActionValue& Value);
// 视角操作回调方法
void Look(const FInputActionValue& Value);
};
IHCPPCharacter.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "IHCPPCharacter.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputSubsystemInterface.h"
// #include "InputActionValue.h"
// #include "InputMappingContext.h"
// Sets default values
AIHCPPCharacter::AIHCPPCharacter()
{
// 初始化弹性臂组件
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent); // 弹性臂组件附加到跟节点
CameraBoom->TargetArmLength = 400.0f; // 弹性臂组件长度
CameraBoom->bUsePawnControlRotation = true; // 控制器能控制弹性臂组件旋转
// 初始化摄像机组件
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // 摄像机组件附加到弹性臂组件上
FollowCamera->bUsePawnControlRotation = false; // 摄像机不相对于弹性臂旋转
// 加载输入配置文件
/* static ConstructorHelpers::FObjectFinder LookInputAction(TEXT("/Game/Input/IA_Look.IA_Look"));
static ConstructorHelpers::FObjectFinder MoveInputAction(TEXT("/Game/Input/IA_Move.IA_Move"));
static ConstructorHelpers::FObjectFinder IMC(TEXT("/Game/Input/IMC_Default.IMC_Default"));
if (LookInputAction.Succeeded() && MoveInputAction.Succeeded() && IMC.Succeeded()) {
LookAction = LookInputAction.Object;
MoveAction = MoveInputAction.Object;
DefaultMappingContext = IMC.Object;
}*/
// 本地找到模型文件
static ConstructorHelpers::FObjectFinder<USkeletalMesh> playerPawn(TEXT("/Script/Engine.SkeletalMesh'/Game/Characters/Mannequins/Meshes/SKM_Quinn_Simple.SKM_Quinn_Simple'"));
if (playerPawn.Succeeded()) {
// 把模型添加给组件的静态网格体
GetMesh()->SetSkeletalMesh(playerPawn.Object);
// 设置网格体位置
GetMesh()->SetRelativeLocation(FVector(0, 0, -90.0f));
// 设置网格体默认方向
GetMesh()->SetRelativeRotation(FRotator(0, -90.0f, 0.f));
}
}
void AIHCPPCharacter::BeginPlay()
{
Super::BeginPlay();
// 加入按键操作绑定功能
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
}
void AIHCPPCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
// 绑定输入操作
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent)) {
// 绑定角色跳输入操作
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
// 绑定移动操作
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AIHCPPCharacter::Move);
// 绑定视角操作
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &AIHCPPCharacter::Look);
}
}
// 移动操作回调方法
void AIHCPPCharacter::Move(const FInputActionValue& Value)
{
FVector2D MovementVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
// 视角操作回调方法
void AIHCPPCharacter::Look(const FInputActionValue& Value)
{
FVector2D LookAxisVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}
编辑 BP_IHCPPCharacter蓝图,指定输入操作如下
编辑 EIHCPPGameMode代码如下:
EIHCPPGameMode.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "EIHCPPGameMode.generated.h"
/**
*
*/
UCLASS()
class EIHCPP_API AEIHCPPGameMode : public AGameModeBase
{
GENERATED_BODY()
public:
AEIHCPPGameMode();
};
EIHCPPGameMode.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "EIHCPPGameMode.h"
#include "IHCPPCharacter.h"
#include "UObject/ConstructorHelpers.h"
AEIHCPPGameMode::AEIHCPPGameMode()
{
static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Script/Engine.Blueprint'/Game/Blueprint/BP_IHCPPCharacter.BP_IHCPPCharacter_C'"));
// 指定默认控制角色
if (PlayerPawnBPClass.Class != NULL)
{
DefaultPawnClass = PlayerPawnBPClass.Class;
}
}