在头文件中添加两个函数,用来接受移动
//前后移动
void MoveForward(float value);
//左右移动
void MoveRight(float value);
定义这两个函数
void ASCharacter::MoveForward(float value)
{
AddMovementInput(GetActorForwardVector(), value);
}
void ASCharacter::MoveRight(float value)
{
AddMovementInput(GetActorRightVector(), value);
}
绑定输入
// 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);
}
编译,然后打开虚幻编辑器的项目设置,添加对应的输入
创建新文件夹,然后保存当前关卡
然后创建角色类的蓝图
拖入主角,并将场景中的游戏开始组件删掉
将场景中的角色组件的控制权进行设置
测试,目前前后左右正常,但是现在鼠标还不能控制方向
但是,character类已经封装好了相关的函数,直接绑定就可以了
//抬头低头
PlayerInputComponent->BindAxis("LookUp", this, &ASCharacter::AddControllerPitchInput);
//转向
PlayerInputComponent->BindAxis("Turn", this, &ASCharacter::AddControllerYawInput);
然后在虚幻里设置映射
==========================
为蓝图中,静态网格找一个模型
添加弹簧臂组件
//弹簧臂
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
class USpringArmComponent* SpringArmComp;
在构造函数中初始化
#include "GameFramework/SpringArmComponent.h"
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
SpringArmComp->bUsePawnControlRotation = true;
SpringArmComp->SetupAttachment(RootComponent);
添加一个摄像机组件
//相机
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
class UCameraComponent* CameraComp;
在构造函数中,初始化
#include "Camera/CameraComponent.h"
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraComponent"));
CameraComp->SetupAttachment(SpringArmComp, USpringArmComponent::SocketName);
编译之后,发现相机和弹簧臂没有绑定成功
所以,我们可以这样做
打开蓝图中的类设置
让他继承父类的父类
然后再改成继承父类
之后效果就正常了
调整弹簧臂位置
弹簧臂长度改成200
然后设置这里
=============================
购买 免费项目
并添加到工程
给网格体换上新的
================
再声明两个函数,来控制执行蹲起动作的开始和结束
//蹲起动作开始
void BeginCrouch();
//蹲起动作结束
void EndCrouch();
定义这两个函数
void ASCharacter::BeginCrouch()
{
//继承于chracter
Crouch();
}
void ASCharacter::EndCrouch()
{
UnCrouch();
}
绑定一下这两个函数
//蹲起
PlayerInputComponent->BindAction("Crouch", IE_Pressed, this, &ASCharacter::BeginCrouch);
PlayerInputComponent->BindAction("Crouch", IE_Released, this, &ASCharacter::EndCrouch);
在构造函数中,设置人物可以蹲
#include "GameFramework/PawnMovementComponent.h"
GetMovementComponent()->GetNavAgentPropertiesRef().bCanCrouch = true;
在ue里面完成绑定
打开蓝图,将这个打上勾
编译测试
发现人物没有蹲,但是镜头蹲下来了
原因是我们的网格体还并没有对应的动画
=============
先给我们的mesh选一个动画蓝图
测试,发现现在还是不能蹲
发现是动画蓝图的问题
在原有蓝图基础上,做出这样的改动就可以了
================
然后设置跳跃
首先在ue里进行绑定
因为character里面本来就实现了该功能 ,我们直接绑定按键就好
//跳跃
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ASCharacter::Jump);
删掉动画蓝图原有的逻辑
然后我们通过if分支语句,当角色是在跳跃状态,且速度小于10,我们就把速度设为0,否则设为实际速度
然后对动画蓝图的跳跃状态变量进行赋值