首先,需要在UE4中设置自己需要输入的按键,点击工具栏的Settings/ProjectSettings,找到input,在Bindings下添加自己需要的输入按键,我这里绑定了JumpBtn、MoveForward、MoveRight、Turn、LookUp
输入设置完毕后,就开始创建自己的主角控制器
一、使用蓝图创建主角
1.在UE4文件夹中,右键创建一个Blueprint Class,选择继承Character(人形控制器),创建后自己命名FirstHero
2.双击打开蓝图,可以给FirstHero添加一个摄像机组件,点击AddComponent,找到Camera
3.开始可以给FirstHero添加控制事件了,打开Event Graph,分别找到最开始设置输入的五个事件,并对应相应的操作,JumpBtn调用Jump跳跃和StopJumping,MoveForward调用AddMoveMentInput前后移动,MoveRight调用AddMoveMentInput左右移动,Turn调用AddControllerYawInput左右旋转,LookUp调用AddControllerPitchInput上下查看
4.还需要一个小小的设置,不然摄像机是不会跟着控制器旋转的,选择摄像机组件,找到CameraSettings/UsePawnControlRotation,选中即可
5.编译,设置好GameMode的DefaultPawnClass,就可以运行了。
<span style="font-size:14px;"><span style="white-space:pre"> </span>static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/BluePrint/FirstHero"));
DefaultPawnClass = PlayerPawnClassFinder.Class;</span>
二、使用C++创建主角
1.在UE4中创建一个C++ Class,选择继承Character(人形控制器),创建后自己命名FirstHero
2.在VS中,进行编辑
#pragma once
#include "GameFramework/Character.h"
#include "FristHero.generated.h"
UCLASS()
class MYNULLPROJECT_API AFristHero : public ACharacter
{
GENERATED_BODY()
class UCameraComponent* FirstCameraCompent;
public:
// Sets default values for this character's properties
AFristHero();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
//前后移动
void MoveForward(float value);
//左右移动
void MoveRight(float value);
//跳跃
void Jump();
//停止跳跃
void StopJump();
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyNULLProject.h"
#include "FristHero.h"
// Sets default values
AFristHero::AFristHero()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
//设置这个控制的Tick函数每帧执行
PrimaryActorTick.bCanEverTick = true;
// 创建一个摄像机组件
FirstCameraCompent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
FirstCameraCompent->AttachParent = GetCapsuleComponent();
FirstCameraCompent->RelativeLocation = FVector(0, 0, 64.f); // Position the camera
//设置摄像机可以跟随控制器旋转
FirstCameraCompent->bUsePawnControlRotation = true;
}
// Called when the game starts or when spawned
void AFristHero::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AFristHero::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
// Called to bind functionality to input
void AFristHero::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
InputComponent->BindAxis("MoveForward", this, &AFristHero::MoveForward);
InputComponent->BindAxis("MoveRight",this, &AFristHero::MoveRight);
InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
InputComponent->BindAction("JumpBtn",IE_Pressed,this, &AFristHero::Jump);
InputComponent->BindAction("JumpBtn", IE_Released, this, &AFristHero::StopJump);
}
//前后移动
void AFristHero::MoveForward(float value)
{
if (value != 0.0f)
{
AddMovementInput(GetActorForwardVector(), value);
}
}
//左右移动
void AFristHero::MoveRight(float value)
{
if (value != 0.0f)
{
AddMovementInput(GetActorRightVector(), value);
}
}
//跳跃
void AFristHero::Jump()
{
Jump();
}
//停止跳跃
void AFristHero::StopJump()
{
StopJumping();
}
3.设置GameMode的DefaultPawnClass
#include "MyNULLProject.h"
#include "MyNULLProjectGameMode.h"
#include "FristHero.h"
AMyNULLProjectGameMode::AMyNULLProjectGameMode()
{
//通过路径找到蓝图,并将蓝图控制器的类设置给DefaultPawnClass
//static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/BluePrint/FirstHero"));
//DefaultPawnClass = PlayerPawnClassFinder.Class;
//如果用C++创建,则直接将控制器的类赋值给DefaultPawnClass
DefaultPawnClass = AFristHero::StaticClass();
}
4.编译运行
三、使用蓝图继承自己写的C++类创建主角
1.参照二写一个C++类
2.在.h文件中的类名上,做一个标注UCLASS(Blueprintable),此类可以为蓝图继承
#pragma once
#include "GameFramework/Character.h"
#include "FristHero.generated.h"
UCLASS(Blueprintable)
class MYNULLPROJECT_API AFristHero : public ACharacter
{
GENERATED_BODY()
class UCameraComponent* FirstCameraCompent;
public:
// Sets default values for this character's properties
AFristHero();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
//前后移动
void MoveForward(float value);
//左右移动
void MoveRight(float value);
//跳跃
void Jump();
//停止跳跃
void StopJump();
};
3.创建一个新蓝图,继承自定义的C++类,可直接搜索到4.蓝图操作,详细见其他资料
5.编译运行