首页 > 编程语言 >UE4 C++实现第三人称角色基本功能

UE4 C++实现第三人称角色基本功能

时间:2022-10-27 19:02:05浏览次数:42  
标签:第三人称 const void float value APCharacter C++ UE4 SpringComp

首先基于Character创建一个角色类,在头文件为其添加弹簧臂和摄像机组件

    UPROPERTY(VisibleAnywhere, Category = "Comp")
        class UCameraComponent* CameraComp;
    UPROPERTY(VisibleAnywhere, Category = "Comp")
        class USpringArmComponent* SpringComp;

在构造函数中将相关组件创建出来

// 所需要添加的头文件
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
        // 创建摄像机组件
    CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
    // 创建弹簧臂组件
    SpringComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringComp"));
    // 将弹簧臂附加到根组件上
    SpringComp->SetupAttachment(RootComponent);
    // 将摄像机组件附加到弹簧臂组件上
    CameraComp->SetupAttachment(SpringComp);
    // 使用Pawn控制旋转
    SpringComp->bUsePawnControlRotation = true;
    CameraComp->bUsePawnControlRotation = true;
    // 如果为真 会跟随控制器移动
    bUseControllerRotationRoll = false;
    bUseControllerRotationYaw = false;
    bUseControllerRotationPitch = false;
    // 旋转朝向移动
    GetCharacterMovement()->bOrientRotationToMovement = true;
    //GetCharacterMovement()->RotationRate = FRotator(0.f, 500.f, 0.f);    

回到角色类头文件中,申明所需的移动、视角旋转函数

    void MoveForward(float value);
    void MoveRight(float value);
    void Turn(float value);
    void LookUp(float value);

实现移动、视角旋转函数,并且绑定输入

void APCharacter::MoveForward(float value)
{
    if (Controller != NULL && value != 0) {
        // 获取控制器旋转
        const FRotator ControllerRotator = Controller->GetControlRotation();
        // 制造一个只有Z轴的旋转
        const FRotator YawRotator(0.f, ControllerRotator.Yaw, 0.f);
        // 获取控制器向前的方向向量 即获取YawRotator的单位长度轴
        const FVector Direction = FRotationMatrix(YawRotator).GetUnitAxis(EAxis::X);
        AddMovementInput(Direction, value);
    }
    
}

void APCharacter::MoveRight(float value)
{
    if (Controller != NULL && value != 0) {
        const FRotator ControllerRotator = Controller->GetControlRotation();
        const FRotator YawRotator(0.f, ControllerRotator.Yaw, 0.f);
        const FVector Direction = FRotationMatrix(YawRotator).GetUnitAxis(EAxis::Y);
        AddMovementInput(Direction, value);
    }
}

void APCharacter::Turn(float value)
{
    if (Controller != NULL && value != 0) {
        AddControllerYawInput(value);
    }
}

void APCharacter::LookUp(float value)
{
    if (Controller != NULL && value != 0) {
        AddControllerPitchInput(value);
    }
}
// Called to bind functionality to input
void APCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    PlayerInputComponent->BindAxis("MoveForward", this, &APCharacter::MoveForward);
    PlayerInputComponent->BindAxis("MoveRight", this, &APCharacter::MoveRight);
    PlayerInputComponent->BindAxis("Turn", this, &APCharacter::Turn);
    PlayerInputComponent->BindAxis("LookUp", this, &APCharacter::LookUp);
}

回到虚幻编辑器中,在项目设置中添加,相关的操作映射

 

 再创建一个相关蓝图类,将模型与动画蓝图设置上就完成了

标签:第三人称,const,void,float,value,APCharacter,C++,UE4,SpringComp
From: https://www.cnblogs.com/limu-zy/p/16833343.html

相关文章