Skip to content

【输入和角色移动】

Markdown 图片

Markdown 图片

使用示例

BlasterCharacter.h

cpp
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "BlasterCharacter.generated.h"

UCLASS()
class BLASTER_API ABlasterCharacter : public ACharacter
{
    GENERATED_BODY()

public:
    ABlasterCharacter();
    virtual void Tick(float DeltaTime) override;
    virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
protected:
    virtual void BeginPlay() override;

    // 定义前后移动
    void MoveForward(float Value);
    // 定义左右移动
    void MoveRight(float Value);
    // 定义控制器盘旋
    void Turn(float Value);
    // 定义拉升俯冲
    void LookUp(float Value);

private:
    UPROPERTY(VisibleAnywhere,Category = Camera)
    class USpringArmComponent* CameraBoom;

    UPROPERTY(VisibleAnywhere, Category = Camera)
    class UCameraComponent* FollowCamera;

public:
};

BlasterCharacter.cpp

cpp
#include "BlasterCharacter.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"

ABlasterCharacter::ABlasterCharacter()
{
    PrimaryActorTick.bCanEverTick = true;

    // 摄像机悬吊
    CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
    CameraBoom->SetupAttachment(GetMesh());// 附着在网格
    CameraBoom->TargetArmLength = 600.f;// 吊杆长度6米
    CameraBoom->bUsePawnControlRotation = true;// 使用棋子控制器进行旋转

    // 悬吊挂上摄像机
    FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
    FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);// 附着在悬吊
    FollowCamera->bUsePawnControlRotation = false;// 不使用棋子控制器控制
}

void ABlasterCharacter::BeginPlay()
{
    Super::BeginPlay();
}

void ABlasterCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    // 绑定按钮输入
    PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
    // 绑定移动输入
    PlayerInputComponent->BindAxis("MoveForward", this, &ThisClass::MoveForward);
    PlayerInputComponent->BindAxis("MoveRight", this, &ThisClass::MoveRight);
    PlayerInputComponent->BindAxis("Turn", this, &ThisClass::Turn);
    PlayerInputComponent->BindAxis("LookUp", this, &ThisClass::LookUp);
}

void ABlasterCharacter::MoveForward(float Value)
{
    if(Controller != nullptr && Value != 0.f)
    {
        // 获取控制器的盘旋值
        const FRotator YawRotation(0.f, Controller->GetControlRotation().Yaw, 0.f);
        // 获取盘旋值对应的X方向(前)转为方向
        const FVector Direction(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X));
        // 给角色进行移动
        AddMovementInput(Direction, Value);
    }
}

void ABlasterCharacter::MoveRight(float Value)
{
    if (Controller != nullptr && Value != 0.f)
    {
        // 获取控制器的盘旋值
        const FRotator YawRotation(0.f, Controller->GetControlRotation().Yaw, 0.f);
        // 获取盘旋值对应的Y方向(右)转为方向
        const FVector Direction(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y));
        // 给角色进行移动
        AddMovementInput(Direction, Value);
    }
}

void ABlasterCharacter::Turn(float Value)
{
    AddControllerYawInput(Value);
}

void ABlasterCharacter::LookUp(float Value)
{
    AddControllerPitchInput(Value);
}

void ABlasterCharacter::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}

MIT Licensed