依然是参考官方文档。
官方的示例代码写的很明白,对于玩家控制的到底是哪个组件?组件到底是相对于谁进行移动?是组件本身还是我们所看向的方向的Yaw方向?官方的代码都写清楚了,斯坦福的教程更多的是一步一步的引导,我个人倒是感觉一次性讲明白会更方便,就没有考虑斯坦福教程的代码,而是看一遍网课理解思路是怎么一步一步的到最终和官方示例代码差不多的那个感觉的。
首先要明白一个概念,那就是所有的组件都有rotation的操作,所有的组件都能够跟随controller的转动而转动,那么我们需要考虑我们希望什么东西跟着我们转动,是根组件吗?还是摄像机?还是摄像机绑定的那个弹簧臂?我想这个问题不难回答,第三人称的实现里面肯定是弹簧臂,回想以下你玩的mmorpg。如果是第一人称的游戏的话,那就是整个character跟随我们的转动而转动了,这和第三人称不一样。
由于踩的坑第一篇文章都踩了,所以这里其实没有什么报错的点。
我唯一有点疑惑的是,官方的实例文档中,JUMP功能的绑定最后的绑定函数的类引用,为什么是ACharacter,而不是我们创建的这个类呢?我尝试了下用我们自己的类去调用这个函数,其实是能够调用到父类的函数的,也许是性能问题?直接调用父类的方法会快一点或者是稳定一点。
上代码
SCharacter.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 "SCharacter.generated.h"
UCLASS(config = Game)
class ACTIONROGUELIKE_API ASCharacter : public ACharacter
{
GENERATED_BODY()
UPROPERTY(VisibleAnywhere)
class USpringArmComponent* SpringArmComp;
UPROPERTY(VisibleAnywhere)
class UCameraComponent* CameraComp;
UPROPERTY(EditAnywhere, Category = Input)
class UInputMappingContext* DefaultMappingContext;
UPROPERTY(EditAnywhere, Category = Input)
class UInputAction* MoveAction;
UPROPERTY(EditAnywhere, Category = Input)
class UInputAction* JumpAction;
UPROPERTY(EditAnywhere, Category = Input)
class UInputAction* LookAction;
public:
// Sets default values for this character's properties
ASCharacter();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
protected:
void Move(const FInputActionValue& Value);
void Look(const FInputActionValue& Value);
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
SCharacter.cpp文件:
// Fill out your copyright notice in the Description page of Project Settings.
#include "SCharacter.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/Controller.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "GameFramework/CharacterMovementComponent.h"
//#include "../../../../../UE_5.1/Engine/Plugins/EnhancedInput/Source/EnhancedInput/Public/EnhancedInputSubsystems.h"
//#include "../../../../../UE_5.1/Engine/Plugins/EnhancedInput/Source/EnhancedInput/Public/EnhancedInputComponent.h"
// Sets default values
ASCharacter::ASCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
bUseControllerRotationPitch = false;
bUseControllerRotationRoll = false;
bUseControllerRotationYaw = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
SpringArmComp->SetupAttachment(RootComponent);
SpringArmComp->bUsePawnControlRotation = true;
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
CameraComp->SetupAttachment(SpringArmComp);
CameraComp->bUsePawnControlRotation = false;
}
// Called when the game starts or when spawned
void ASCharacter::BeginPlay()
{
Super::BeginPlay();
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
}
void ASCharacter::Move(const FInputActionValue& Value)
{
FVector2D MovementVector = Value.Get<FVector2D>();
if (Controller!=nullptr)
{
const FRotator Rotation = Controller->GetControlRotation();
//拿到控制器的Yaw面的旋转角度,而不是Character的旋转角度
const FRotator YawRotation(0, Rotation.Yaw, 0);
//把角度用向量来表示,同时获取到不同方向的向量值,而这个向量的角度是按照我们控制器的角度来说的
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
//MovementVector的X和Y是相对于我们的视角来说的,也就是我们视角的正前方是Y,而我们视角的正右方是X
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
void ASCharacter::Look(const FInputActionValue& Value)
{
FVector2D LookAxisVector = Value.Get<FVector2D>();
if (Controller!=nullptr)
{
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}
// Called every frame
void ASCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent))
{
//MoveAction
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ASCharacter::Move);
//JumpAction
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
//LookAction
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ASCharacter::Look);
}
}