任务:创建C++角色类,并增加控制角色移动跳跃,鼠标镜头转向功能
第一步、项目设置输入添加相应的轴映射和操作映射
第二步、新建C++角色类Man,并修改相应代码
Man.h代码如下:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Man.generated.h"
UCLASS()
class LEARNTEST_API AMan : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMan();
//弹簧臂组件
UPROPERTY(EditAnywhere)
class USpringArmComponent* SpringArm;
//摄像头组件
UPROPERTY(EditAnywhere)
class UCameraComponent* Camera;
//镜头Yaw方向转向率
UPROPERTY(EditAnywhere)
float BasicTurnRate;
//镜头Pitch方向转向率
UPROPERTY(EditAnywhere)
float BasicLookupRate;
private:
//前后移动函数
UFUNCTION()
void MoveForward(float Value);
//左右移动函数
UFUNCTION()
void MoveRight(float Value);
//镜头Yaw方向转向函数
UFUNCTION()
void Turn(float Value);
//镜头Pitch方向转向函数
UFUNCTION()
void Lookup(float Value);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
Man.cpp代码如下:(注意:为了使用GetCharacterMovement()->bOrientRotationToMovement,必须添加头文件 "GameFramework\Character.h"和"GameFramework\CharacterMovementComponent.h"否则编译时会报错)
#include "Man.h"
#include "GameFramework\SpringArmComponent.h"
#include "Camera\CameraComponent.h"
#include "Components\StaticMeshComponent.h"
#include "GameFramework\Character.h"
#include "GameFramework\CharacterMovementComponent.h"
// Sets default values
AMan::AMan()
{
// 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;
// 初始化弹簧臂组件,将其绑定在根组件上,且其旋转由PawnController控制
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
SpringArm->SetupAttachment(RootComponent);
SpringArm->TargetArmLength = 300.f;
SpringArm->bEnableCameraLag = true;
SpringArm->bUsePawnControlRotation = true;
// 初始化摄像头组件,将其绑定在弹簧臂的另一端
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
Camera->SetupAttachment(SpringArm);
Camera->bUsePawnControlRotation = false;
// 初始化两个旋转率
BasicTurnRate = 65;
BasicLookupRate = 65;
// 角色的旋转不由Controller控制
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// 设置角色朝向为移动方向
GetCharacterMovement()->bOrientRotationToMovement = true;
// 将该Character设为由最小编号玩家控制
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// 将输入映射和函数绑定
void AMan::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &AMan::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AMan::MoveRight);
PlayerInputComponent->BindAxis("CameraYaw", this, &AMan::Turn);
PlayerInputComponent->BindAxis("CameraPitch", this, &AMan::Lookup);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AMan::Jump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &AMan::StopJumping);
}
// 前后移动函数
void AMan::MoveForward(float Value)
{
// 设置移动方向为Controller的X轴方向
FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
// 左右移动函数
void AMan::MoveRight(float Value)
{
//设置移动方向为Controller的Y轴方向
FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
AddMovementInput(Direction, Value);
}
// 控制器Yaw方向转向函数,其灵敏度由BasicTurnRate参数决定
void AMan::Turn(float Value)
{
AddControllerYawInput(Value * BasicTurnRate * GetWorld()->DeltaTimeSeconds);
}
// 控制器Pitch方向转向函数,其灵敏度由BasicLookupRate参数决定
void AMan::Lookup(float Value)
{
AddControllerPitchInput((-1)*Value * BasicLookupRate * GetWorld()->DeltaTimeSeconds);
}
// Called when the game starts or when spawned
void AMan::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMan::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}