心血来潮想看下更新增强上下文映射输入之后ue的c++控制的人物移动该怎么实现,于是创建了一个新的C++文件,拿到官方的示例进行分析,分析结果如下:
.h头文件
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "InputActionValue.h"
#include "MyProject01Character.generated.h"
UCLASS(config=Game)
class AMyProject01Character : public ACharacter
{
GENERATED_BODY()
/** Camera boom positioning the camera behind the character */
//弹簧臂组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom;
/** Follow camera */
//摄像机组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera;
//输入操作,事件
/** MappingContext */
//输入映射上下文
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputMappingContext* DefaultMappingContext;
/** Jump Input Action */
//跳跃输入
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* JumpAction;
/** Move Input Action */
//移动输入
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* MoveAction;
/** Look Input Action */
//视角控制
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* LookAction;
public:
//初始化函数
AMyProject01Character();
protected:
//输入调用函数
/** Called for movement input */
void Move(const FInputActionValue& Value);
/** Called for looking input */
void Look(const FInputActionValue& Value);
protected:
// APawn interface
//函数重写
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// To add mapping context
//beginplay事件,用来添加上下文映射
virtual void BeginPlay();
public:
//摄像机组件和弹簧臂组件的获取方法,公开
/** Returns CameraBoom subobject **/
FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
/** Returns FollowCamera subobject **/
FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }
};
.cpp文件
// Copyright Epic Games, Inc. All Rights Reserved.
#include "MyProject01Character.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
//
// AMyProject01Character
//初始化
AMyProject01Character::AMyProject01Character()
{
// Set size for collision capsule
//设置胶囊体大小
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
// Don't rotate when the controller rotates. Let that just affect the camera.
//让控制器的转动不影响角色的转动,而是只影响摄像机的转动
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// Configure character movement、
//bOrientRotationToMovement:为真时,让角色面朝加速度的方向;
//RotationRate:角色转向的速度,数值越高,转速越快
GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
GetCharacterMovement()->RotationRate = FRotator(0.0f, 5000.0f, 0.0f); // ...at this rotation rate
// Note: For faster iteration times these variables, and many more, can be tweaked in the Character Blueprint
// instead of recompiling to adjust them
//跳跃垂直加速度
GetCharacterMovement()->JumpZVelocity = 700.f;
//角色在空中的控制量,如果为1则为完全控制,如果为0则完全不能控制
GetCharacterMovement()->AirControl = 0.35f;
//角色最大行走速度
GetCharacterMovement()->MaxWalkSpeed = 500.f;
//角色最小模拟行走速度,用来模拟当摇杆推动的角度特别小的时候人物的行走速度
GetCharacterMovement()->MinAnalogWalkSpeed = 20.f;
//行走时的减速度,以一个常量值降低速度
GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;
// Create a camera boom (pulls in towards the player if there is a collision)
//声明一个弹簧臂
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
//将弹簧臂作为根组件的子组件
CameraBoom->SetupAttachment(RootComponent);
//设置臂长
CameraBoom->TargetArmLength = 400.0f; // The camera follows at this distance behind the character
//让控制器的转动影响弹簧臂的转动
CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
// Create a follow camera
//声明一个摄像机
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
// Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
//将摄像机组件作为弹簧臂组件的子组件,加在弹簧臂的端点上
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
//摄像头不受控制器的转动的影响
FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
// are set in the derived blueprint asset named ThirdPersonCharacter (to avoid direct content references in C++)
}
void AMyProject01Character::BeginPlay()
{
// Call the base class
Super::BeginPlay();
//Add Input Mapping Context
//当前pawn的控制器转换为玩家控制器,也就是让玩家控制这个角色,并判断控制器visible
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
//获取玩家控制器,并从玩家控制器中获取增强输入本地玩家子系统
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
//把增强映射上下文资产给丢进去,并且设优先级为0(最低,优先级数字越高,优先级越高)
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
}
//
// Input
//重写SetupPlayerInputComponent,实现接口
void AMyProject01Character::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
// Set up action bindings
//玩家输入组件转换为增强输入组件类并赋值给EnhancedInputComponent
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent)) {
//Jumping
//摁下跳跃键的时候,进行跳跃,放开跳跃键的时候停止跳跃
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
//Moving
//绑定移动输入事件
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AMyProject01Character::Move);
//Looking
//绑定视角输入事件
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &AMyProject01Character::Look);
}
}
void AMyProject01Character::Move(const FInputActionValue& Value)
{
// input is a Vector2D
//输入值其实是一个2d向量,所以我们最后也要把它转化成2d向量,这个2d向量只会有三个值(如果是鼠标键盘的话),就是0、1和-1
FVector2D MovementVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
// find out which way is forward
//找到控制器的旋转输入,拿到绕z轴旋转的方向也就是控制器的朝向方向
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
//找到人物此时的前向量
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
// get right vector
//找到人物此时的右向量
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement
//增加输入
//当我们摁下A或D的时候,MovementVector.Y的值就不是0,就会按照摄像机的方向的左右进行移动
//当我们摁下W或S的时候也是。
//AddMovementInput:沿给定场景方向向量添加运动输入
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
void AMyProject01Character::Look(const FInputActionValue& Value)
{
// input is a Vector2D
//获得鼠标方向
FVector2D LookAxisVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
// add yaw and pitch input to controller
//将鼠标的位置添加到控制器的controllerRotation
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}
注意:其中有部分组件在当前.h文件与.cpp文件中未定义,原因是因为其父类定义了相应的组件,子类继承父类自然就有了这些东西,想找到更详细的组件定义的话就去character类和pawn类甚至是object类里面找。