介绍:
跟随B站UP主技术宅阿棍儿的转述教程视频,制作的笔记加上自己的理解.
原教程链接:CoopGame(课程已停止注册)
需要准备的资产:项目源文件(蓝奏云),密码:zmingu
UE版本:4.27.2
VS版本:2022

创建项目

  1. 新建C++空项目,取消初学者内容包,设置项目名:CoopGame.

    CoopGame01-创建玩家

创建角色

创建角色类

  1. 创建继承Character类的SCharacter玩家角色类,设置为public.

    CoopGame01-创建玩家

角色移动

  1. 编写移动函数.

    /*SCharacter.h*/
    //移动函数
    void MoveForward(float Value);
    void MoveRight(float Value);
    /*SCharacter.cpp*/
    void ASCharacter::MoveForward(float Value)
    {
        AddMovementInput(GetActorForwardVector(),Value);
    }
    
    void ASCharacter::MoveRight(float Value)
    {
        AddMovementInput(GetActorRightVector(),Value);
    }
  2. 设置按键轴映射,并绑定移动函数.

    1. 按键映射在:项目设置→引擎→输入,MoveForward,MoveRight
    2. 问:为什么S键和A键的的缩放需要设置为-1?

    CoopGame01-创建玩家

    /*SCharacter.cpp*/
    /*SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)函数添加*/
    PlayerInputComponent->BindAxis("MoveForward",this,&ASCharacter::MoveForward);
    PlayerInputComponent->BindAxis("MoveRight",this,&ASCharacter::MoveRight);

角色视角

  1. 轴映射绑定UE自带的控制视角旋转函数.LookUp,Turn

    1. 问:为什么鼠标Y的缩放要设置为-1?

CoopGame01-创建玩家


```cpp
/*SCharacter.cpp*/
/*SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)函数添加*/
PlayerInputComponent->BindAxis("LookUp",this,&ASCharacter::AddControllerPitchInput);
PlayerInputComponent->BindAxis("Turn",this,&ASCharacter::AddControllerYawInput);

```

摄像机和弹簧臂

  1. 创建摄像机和弹簧臂组件并初始化
/*SCharacter.h*/
//摄像机和弹簧臂组件
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
class UCameraComponent* CameraComp;

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
class USpringArmComponent* SpringArmComp;
/*SCharacter.cpp*/
/*添加摄像机和弹簧臂组件引用*/
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"

/*ASCharacter()构造函数添加*/
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
SpringArmComp->bUsePawnControlRotation = true;
SpringArmComp->SetupAttachment(RootComponent);
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComp"));
CameraComp->SetupAttachment(SpringArmComp);

角色下蹲和跳跃

  1. 编写并用按键操作映射绑定下蹲函数。Crouch,Jump

    CoopGame01-创建玩家

    /*SCharacter.h*/
    
    //下蹲和结束下蹲函数
    void BeginCrouch();
    void EndCrouch();
    /*SCharacter.cpp*/
    
    /*引入移动组件*/
    #include "GameFramework/PawnMovementComponent.h"
    /*ASCharacter()构造函数添加*/
    GetMovementComponent()->GetNavAgentPropertiesRef().bCanCrouch = true;//设置角色允许下蹲
    
    /*SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)函数添加*/
    PlayerInputComponent->BindAction("Crouch",IE_Pressed,this,&ASCharacter::BeginCrouch);
    PlayerInputComponent->BindAction("Crouch",IE_Released,this,&ASCharacter::EndCrouch);
    PlayerInputComponent->BindAction("Jump",IE_Pressed,this,&ASCharacter::Jump);
    PlayerInputComponent->BindAction("Jump",IE_Released,this,&ASCharacter::StopJumping);
    
    void ASCharacter::BeginCrouch()
    {
        Crouch();
    }
    
    void ASCharacter::EndCrouch()
    {
        UnCrouch();
    }

角色动画

  1. 导入虚幻商城免费资源动画初学者内容包/Animation Starter Pack,添加到工程

    CoopGame01-创建玩家

  2. 创建蓝图类BP_SCharacter:继承SCharacterC++类
  3. 设置网格体为默认小白人,设置位置Z为-90,旋转Z为-90,设置动画蓝图为UE4ASP_HeroTPP_AnimBlueprint

    CoopGame01-创建玩家

    CoopGame01-创建玩家

  4. 修改UE4ASP_HeroTPP_AnimBlueprint动画蓝图的事件图表如下,实现蹲和跳跃的动画被角色类中的函数驱动。(双击放大图片)

    CoopGame01-创建玩家

效果测试

  1. 创建蓝图类BP_CoopGameGameMode,继承CoopGameGameModeBaseC++类
  2. 在场景设置中设置GameMode为BP_CoopGameGameMode,设置其中的默认Pawn类为BP_SCharacter

    CoopGame01-创建玩家

  3. 运行查看结果

    CoopGame01-创建玩家

文章目录