02uec++多人游戏【角色创建和动作生成】

在头文件中添加两个函数,用来接受移动

	//前后移动
	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,否则设为实际速度

 然后对动画蓝图的跳跃状态变量进行赋值 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无情的阅读机器

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值