这一篇是进行玩家移动和视角移动的介绍。
1.在玩家内进行移动覆写
virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;
2.创建增强输入资产的变量创建
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "CharacterData", meta = (AllowPrivateAccess = "true"))UDA_InputConfig* InputConfigDataAsset;
3.进行移动和视角移动函数的创建
void Input_Move(const FInputActionValue& InputActionValue);void Input_Look(const FInputActionValue& InputActionValue);
4.在SetupPlayerInputComponent函数内进行输入绑定
//设置玩家输入组件
void AXMBCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{//添加映射上下文checkf(InputConfigDataAsset, TEXT("Forgot to assign a valid data as input config"))//获取本地玩家ULocalPlayer* LocalPlayer = GetController<APlayerController>()->GetLocalPlayer();//获取增强输入子系统UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(LocalPlayer);check(Subsystem);//添加映射上下文Subsystem->AddMappingContext(InputConfigDataAsset->DefaultMappingContext,0);//获取角色输入组件并绑定输入动作UXMBWarriorInputComponent* WarriorInputComponent = CastChecked<UXMBWarriorInputComponent>(PlayerInputComponent);WarriorInputComponent->BindNativeInputAction(InputConfigDataAsset,XMBGameplayTags::InputTag_Move, ETriggerEvent::Triggered,this,&ThisClass::Input_Move);WarriorInputComponent->BindNativeInputAction(InputConfigDataAsset,XMBGameplayTags::InputTag_Look, ETriggerEvent::Triggered,this,&ThisClass::Input_Look);}
5.对Move函数进行设置
void AXMBCharacter::Input_Move(const FInputActionValue& InputActionValue)
{//获取移动向量const FVector2D MovementVector = InputActionValue.Get<FVector2D>();//获取移动旋转const FRotator MovementRotation(0.f, Controller->GetControlRotation().Yaw, 0.f);//处理前后移动if (MovementVector.Y != 0.f){const FVector ForwardDirection = MovementRotation.RotateVector(FVector::ForwardVector);AddMovementInput(ForwardDirection, MovementVector.Y);}//处理左右移动if (MovementVector.X != 0.f){const FVector RightDirection = MovementRotation.RotateVector(FVector::RightVector);AddMovementInput(RightDirection, MovementVector.X);}}
6.对视角移动函数进行设置
void AXMBCharacter::Input_Look(const FInputActionValue& InputActionValue)
{const FVector2D LookAxisVector = InputActionValue.Get<FVector2D>();if (LookAxisVector.X != 0.f){AddControllerYawInput(LookAxisVector.X);}if (LookAxisVector.Y != 0.f){AddControllerPitchInput(LookAxisVector.Y);}
}
7.打开引擎,进入角色蓝图,细节面板搜索data,更换成自己的dataasset
#include "Character/XMBCharacter.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "EnhancedInputSubsystems.h"
#include "Data/Input/DA_InputConfig.h"
#include "Components/Input/XMBWarriorInputComponent.h"
#include "XMBGameplayTags.h"#include "XMBDebugHelper.h"AXMBCharacter::AXMBCharacter()
{GetCapsuleComponent()->InitCapsuleSize(42.f, 96.f);bUseControllerRotationPitch = false;bUseControllerRotationYaw = false;bUseControllerRotationRoll = false;CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));CameraBoom->SetupAttachment(GetRootComponent());CameraBoom->TargetArmLength = 300.0f;CameraBoom->SocketOffset = FVector(0.0f, 55.0f, 60.0f);CameraBoom->bUsePawnControlRotation = true;FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);FollowCamera->bUsePawnControlRotation = false;GetCharacterMovement()->bOrientRotationToMovement = true;GetCharacterMovement()->RotationRate = FRotator(0.0f, 550.0f, 0.0f);GetCharacterMovement()->MaxWalkSpeed = 400.f;GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;}void AXMBCharacter::BeginPlay()
{Super::BeginPlay();Debug::Print(FString::Printf(TEXT("XMBCharacter::BeginPlay")));
}//设置玩家输入组件
void AXMBCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{//添加映射上下文checkf(InputConfigDataAsset, TEXT("Forgot to assign a valid data as input config"))//获取本地玩家ULocalPlayer* LocalPlayer = GetController<APlayerController>()->GetLocalPlayer();//获取增强输入子系统UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(LocalPlayer);check(Subsystem);//添加映射上下文Subsystem->AddMappingContext(InputConfigDataAsset->DefaultMappingContext,0);//获取角色输入组件并绑定输入动作UXMBWarriorInputComponent* WarriorInputComponent = CastChecked<UXMBWarriorInputComponent>(PlayerInputComponent);WarriorInputComponent->BindNativeInputAction(InputConfigDataAsset,XMBGameplayTags::InputTag_Move, ETriggerEvent::Triggered,this,&ThisClass::Input_Move);WarriorInputComponent->BindNativeInputAction(InputConfigDataAsset,XMBGameplayTags::InputTag_Look, ETriggerEvent::Triggered,this,&ThisClass::Input_Look);}void AXMBCharacter::Input_Move(const FInputActionValue& InputActionValue)
{//获取移动向量const FVector2D MovementVector = InputActionValue.Get<FVector2D>();//获取移动旋转const FRotator MovementRotation(0.f, Controller->GetControlRotation().Yaw, 0.f);//处理前后移动if (MovementVector.Y != 0.f){const FVector ForwardDirection = MovementRotation.RotateVector(FVector::ForwardVector);AddMovementInput(ForwardDirection, MovementVector.Y);}//处理左右移动if (MovementVector.X != 0.f){const FVector RightDirection = MovementRotation.RotateVector(FVector::RightVector);AddMovementInput(RightDirection, MovementVector.X);}// if (MovementVector.Z != 0.f)// {// const FVector RightDirection = MovementRotation.RotateVector(FVector::UpVector);//// AddMovementInput(RightDirection, MovementVector.Z);// }}void AXMBCharacter::Input_Look(const FInputActionValue& InputActionValue)
{const FVector2D LookAxisVector = InputActionValue.Get<FVector2D>();if (LookAxisVector.X != 0.f){AddControllerYawInput(LookAxisVector.X);}if (LookAxisVector.Y != 0.f){AddControllerPitchInput(LookAxisVector.Y);}
}
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "InputActionValue.h"
#include "Character/CharacterBase.h"
#include "XMBCharacter.generated.h"class UDA_InputConfig;
class UCameraComponent;
class USpringArmComponent;
struct FInputActionValue;
/*** */
UCLASS()
class ARPG_GRIVITY_API AXMBCharacter : public ACharacterBase
{GENERATED_BODY()
public:AXMBCharacter();protected:virtual void BeginPlay() override;virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;private:UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))USpringArmComponent* CameraBoom;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))UCameraComponent* FollowCamera;//UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "CharacterData", meta = (AllowPrivateAccess = "true"))UDA_InputConfig* InputConfigDataAsset;void Input_Move(const FInputActionValue& InputActionValue);void Input_Look(const FInputActionValue& InputActionValue);};