UE4 创建自己的角色

首先,需要在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.编译运行


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

神码编程

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

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

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

打赏作者

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

抵扣说明:

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

余额充值