介绍:
跟随B站UP主技术宅阿棍儿的转述教程视频,制作的笔记加上自己的理解.
原教程链接:CoopGame(课程已停止注册)
需要准备的资产:项目源文件(蓝奏云),密码:zmingu
UE版本:4.27.2
VS版本:2022
创建项目
创建角色
创建角色类
角色移动
编写移动函数.
/*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); }
设置按键轴映射,并绑定移动函数.
- 按键映射在:项目设置→引擎→输入,
MoveForward
,MoveRight
- 问:为什么S键和A键的的缩放需要设置为-1?
/*SCharacter.cpp*/ /*SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)函数添加*/ PlayerInputComponent->BindAxis("MoveForward",this,&ASCharacter::MoveForward); PlayerInputComponent->BindAxis("MoveRight",this,&ASCharacter::MoveRight);
- 按键映射在:项目设置→引擎→输入,
角色视角
轴映射绑定UE自带的控制视角旋转函数.
LookUp
,Turn
- 问:为什么鼠标Y的缩放要设置为-1?
```cpp
/*SCharacter.cpp*/
/*SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)函数添加*/
PlayerInputComponent->BindAxis("LookUp",this,&ASCharacter::AddControllerPitchInput);
PlayerInputComponent->BindAxis("Turn",this,&ASCharacter::AddControllerYawInput);
```
摄像机和弹簧臂
- 创建摄像机和弹簧臂组件并初始化
/*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);
角色下蹲和跳跃
编写并用按键操作映射绑定下蹲函数。
Crouch
,Jump
/*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(); }
角色动画
导入虚幻商城免费资源动画初学者内容包/Animation Starter Pack,添加到工程
- 创建蓝图类
BP_SCharacter
:继承SCharacter
C++类 设置网格体为默认小白人,设置位置Z为-90,旋转Z为-90,设置动画蓝图为
UE4ASP_HeroTPP_AnimBlueprint
修改UE4ASP_HeroTPP_AnimBlueprint动画蓝图的事件图表如下,实现蹲和跳跃的动画被角色类中的函数驱动。(双击放大图片)