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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 所需要添加的头文件
#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);

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

1
2
3
4
void MoveForward(float value);
void MoveRight(float value);
void Turn(float value);
void LookUp(float value);

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
}

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

Axis Mappings
    MoveForward
        W     Scale 1.0
        S    Scale -1.0
    MoveRight
        D    Scale 1.0
        A    Scale -1.0
    Turn
        Mouse X    Scale 1.0
    LookUp
        Mouse Y    Scale -1.0

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