引擎版本:UE 4.22 + VS 2019; 学习教程:Unreal Engine 4 Mastery - Create Multiplayer Games With Cpp
1、创建一个UE4,C++Basic工程(CoopGame),创建一个Character(SCharacter)
2、添加基础移动控制:对应的在头文件中添加MoveRight和MoveForward方法,并且在Editor的projectSetting中对应设置Input对应的按键
// Called to bind functionality to input
void ASCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &ASCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ASCharacter::MoveRight);
PlayerInputComponent->BindAxis("LookUp", this, &ASCharacter::AddControllerPitchInput);
PlayerInputComponent->BindAxis("Turn", this, &ASCharacter::AddControllerYawInput);}
void ASCharacter::MoveRight(float Value)
{
AddMovementInput(GetActorRightVector(), Value);
}void ASCharacter::MoveForward(float Value)
{
AddMovementInput(GetActorForwardVector(), Value);
}
3、编译,上传至Git存储,初始工程大小为5K左右
4、添加相机组件
(一)、初步,添加一个UCameraComponent,并实例化该Component,注意添加头文件和前置class声明
UPROPERTY(VisibleAnywhere,BlueprintReadOnly,Category=Component)
class UCameraComponent* CameraComp;对应.cpp产生一个CameraComponent实例
#include "Camera/CameraComponent.h"
ASCharacter::ASCharacter()
{
// 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;
CameraComp = CreateDefaultSubobject(TEXT("Components"));
CameraComp->bUsePawnControlRotation = true;//使用Pawn的旋转控制相机的移动
}
(二)、进一步,添加USpringArmComponent,该组件相当于自拍杆
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Component)
class USpringArmComponent* SpringArmComp;对应的.cpp修改如下
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
// Sets default values
ASCharacter::ASCharacter()
{
// 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;SpringArmComp = CreateDefaultSubobject
(TEXT("SpringArmComponent"));
SpringArmComp->bUsePawnControlRotation = true;
SpringArmComp->SetupAttachment(RootComponent);
CameraComp = CreateDefaultSubobject(TEXT("Components"));
CameraComp->SetupAttachment(SpringArmComp);
}
(三)、基于SCharacter创建蓝图(BP_SCharacter),适当调整SpringArm的位置
5、导入animation starter pack.第二次push,仓库增加20M
6、BP_SCharacter中指定Mesh,调整位置
7、为角色添加下蹲动作,对应添加BeginCrouch,EndCrouch函数声明
PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ASCharacter::BeginCrouch);
PlayerInputComponent->BindAction("Crouch", IE_Released, this, &ASCharacter::EndCrouch);void ASCharacter::BeginCrouch()
{
Crouch(); //调用Character中提供的Crouch方法,需要在Character中启用Crouch
}
void ASCharacter::EndCrouch()
{
UnCrouch();
}
编译,在蓝图BP_SCharacter中启用Crouch
8、添加Jump,
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ASCharacter::Jump);
9、添加动画(这里暂时不涉及具体动画蓝图的编写)
(一)基于UE4ASP_HeroTPP_AnimBlueprint复制一份动画蓝图,修改名称PlayerPawn_AnimBP;原先的动画蓝图已经提供了全部所需的功能
(二)删除其中和BP_SCharacter不对应的部分,包含Enable Jump整块和Crouching的设置
(三)为BP_SCharacter添加动画蓝图
(四)设置Crouching(CastToCharacter->Get Is Crouch->Set Crouching)
(五) 设置Enable Jump (CastToCharacter->Get Was Jumping->Set EnableJumping)
最终左图为BP_SCharacter,右图为Ue4ASP_Character,可以进行相同的控制