当你运行我们上次做完的项目,你可能会意识到我们移动的摄像机还是默认的那个摄像机,这个默认的摄像机可以自由飞翔。这一节,我们要使得开始的角色是我们的一个Avatar类的实例对象,并且使用键盘控制我们的角色。创建游戏模式类1. 创建游戏模式的步骤如下: 1) 点击文件 --> 新建C++类。 2) 选择Game Mode(游戏模式)。 3) 将其命名为“MyGameMode1”。 4) 点击创建类。 什么是GameMode?GameMode包含了各种各样的游戏规则和让游戏引擎描述这个游戏是怎么玩的。
2. 创建游戏模式的蓝图 UE4会自动启动VS开发环境,然后我们来创建MyGameMode1蓝图:
1) 如图所示操作: 2) 填写蓝图名称,我这里是“BP_GameMode1”,然后点好。 3) 从右侧的细节面板中的Default Pawn Class的下拉选项中选择上次我们创建好的角色蓝图BP_Avatar。 什么是Default Pawn Class?Default Pawn Class就是被角色使用的那一类物体,也就是可以被玩家控制的Actor角色。
4) 点击工具栏的保存,然后退出。
现在运行游戏的话,你可以看到我们使用的摄像头已经是BP_Avatar角色所包含的摄像头了。但是现在还是控制不了角色,因为我们还没设置控制器输入。设置检测键盘输入1) 点击工具栏的设置,然后点击项目设置。

2) 接下来,点击左侧面板的输入,然后在Axis Mappings(按键映射)后面点击加号,再点击前面的小三角形展开。输入一个名为Forward(前进)的按键映射,然后下面选择W键。接着再添加一个名为Back(后退)的按键映射,然后下面选择D键。Left(左移)对应A键,Right(右移)对于D键。 3) 直接关闭该窗口以保存设置。通过C++代码控制角色行走1) 现在打开你的VS里面的Avatar.h构造器,添加五个成员函数的声明:- void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
- void MoveForward(float amount);
- void MoveBack(float amount);
- void MoveLeft(float amount);
- void MoveRight(float amount);
并删除原有的这一行:virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override; 2) 然后在Avatar.cpp完成函数体定义:- void AAvatar::SetupPlayerInputComponent(class UInputComponent* InputComponent)
- {
- check(InputComponent);
- InputComponent->BindAxis("Forward", this, &AAvatar::MoveForward);
- InputComponent->BindAxis("Back", this, &AAvatar::MoveForward);
- InputComponent->BindAxis("Left", this, &AAvatar::MoveRight);
- InputComponent->BindAxis("Right", this, &AAvatar::MoveRight);
- }
上面的InputComponent::BindAxis(...)函数用于将按键信息于函数调用绑定。例如当玩家按下W键,引擎就会检测到有我们之前命名的"Forward"按键信息,然后自动去调用当前类的AAvatar::MoveForward(float amount)函数。其它三个按键也是如此运作。所以接下来定义好这四个函数就可以了,这里的参数amount是设备输入量经过与scale相乘后得出的结果:- void AAvatar::MoveForward(float amount)
- {
-
-
- if (Controller && amount)
- {
-
- FVector fwd = GetActorForwardVector();
-
- AddMovementInput(fwd, amount);
- }
- }
- void AAvatar::MoveBack(float amount)
- {
- if (Controller && amount)
- {
-
- FVector back = -GetActorForwardVector();
- AddMovementInput(back, amount);
- }
- }
- void AAvatar::MoveLeft(float amount)
- {
- if (Controller && amount)
- {
- FVector left = -GetActorRightVector();
- AddMovementInput(left, amount);
- }
- }
- void AAvatar::MoveRight(float amount)
- {
- if (Controller && amount)
- {
- FVector right = GetActorRightVector();
- AddMovementInput(right, amount);
- }
- }
设置检测鼠标移动接下来我们用第二步同样的操作打开项目设置并添加Yaw和Pitch按键信息,分别对应的是鼠标的X坐标和Y坐标。
注意Yaw的意思是绕竖轴旋转,Pitch的意思是绕横向轴旋转。见下图:
 通过C++代码控制角色镜头在Avatar.h你需要添加两个函数声明:- void Yaw(float amount);
- void Pitch(float amount);
然后在Avatar.cpp中实现它们:- void AAvatar::Yaw(float amount)
- {
- if (Controller && amount)
- {
-
-
-
-
-
- AddControllerYawInput(200.f * amount * GetWorld()->GetDeltaSeconds());
- }
- }
- void AAvatar::Pitch(float amount)
- {
- if (Controller && amount)
- {
- AddControllerPitchInput(200.f * amount * GetWorld()->GetDeltaSeconds());
- }
- }
在void AAvatar::SetupPlayerInputComponent(class UInputComponent* InputComponent)函数体的下面添加这两条语句将输入信息和函数绑定:- InputComponent->BindAxis("Yaw", this, &AAvatar::Yaw);
- InputComponent->BindAxis("Pitch", this, &AAvatar::Pitch);
完整代码贴出Avatar.h完整代码如下:- #pragma once
- #include "GameFramework/Character.h"
- #include "Avatar.generated.h"
- UCLASS()
- class DEMO1_API AAvatar : public ACharacter
- {
- GENERATED_BODY()
-
- void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
- void MoveForward(float amount);
- void MoveBack(float amount);
- void MoveLeft(float amount);
- void MoveRight(float amount);
- void Yaw(float amount);
- void Pitch(float amount);
- public:
-
- AAvatar();
-
- virtual void BeginPlay() override;
-
-
- virtual void Tick( float DeltaSeconds ) override;
- };
Avatar.cpp完整代码如下:- #include "Demo1.h"
- #include "Avatar.h"
- AAvatar::AAvatar()
- {
-
- PrimaryActorTick.bCanEverTick = true;
- }
- void AAvatar::BeginPlay()
- {
- Super::BeginPlay();
- }
- void AAvatar::Tick( float DeltaTime )
- {
- Super::Tick( DeltaTime );
- }
- void AAvatar::SetupPlayerInputComponent(class UInputComponent* InputComponent)
- {
- check(InputComponent);
- InputComponent->BindAxis("Forward", this, &AAvatar::MoveForward);
- InputComponent->BindAxis("Back", this, &AAvatar::MoveForward);
- InputComponent->BindAxis("Left", this, &AAvatar::MoveRight);
- InputComponent->BindAxis("Right", this, &AAvatar::MoveRight);
- InputComponent->BindAxis("Yaw", this, &AAvatar::Yaw);
- InputComponent->BindAxis("Pitch", this, &AAvatar::Pitch);
- }
- void AAvatar::MoveForward(float amount)
- {
-
-
- if (Controller && amount)
- {
-
- FVector fwd = GetActorForwardVector();
-
- AddMovementInput(fwd, amount);
- }
- }
- void AAvatar::MoveBack(float amount)
- {
- if (Controller && amount)
- {
-
52VR.COM微信扫一扫
专注于VR的学习、开发和人才交流
|