UE4 创建第三人称角色

一、C++创建

1.      创建一个C++类,继承自Character,取名MyThirdCharacter

2.      在头文件中声明一个摄像机目标点CameraBoom,一个摄像机FollowCamera,再声明两个控制移动的函数,与跳跃的函数

#include "GameFramework/Character.h"
#include "MyThirdCharacter.generated.h"

UCLASS()
class MYFIRSTNULLPROJECT_API AMyThirdCharacter : public ACharacter
{
	GENERATED_BODY()
	
	//摄像机目标点
	class USpringArmComponent* CameraBoom;
	//跟随相机
	class UCameraComponent* FollowCamera;
public:
	// Sets default values for this character's properties
	AMyThirdCharacter();

	// 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);
};

3.      在构造函数中,初始化一些控制参数与创建自己所需要的组件

4.      在输入中,处理控制函数

#include "MyFirstNULLProject.h"
#include "MyThirdCharacter.h"

// Sets default values
AMyThirdCharacter::AMyThirdCharacter()
{
 	// 设置这个Character每帧调用Tick函数
	PrimaryActorTick.bCanEverTick = true;
	//设置控制器不受旋转影响
	bUseControllerRotationPitch = false;
	bUseControllerRotationRoll = false;
	bUseControllerRotationYaw = false;
	//设置胶囊碰撞体的大小
	GetCapsuleComponent()->InitCapsuleSize(42.f, 96.f);
	//设置控制器在移动时可以旋转
	GetCharacterMovement()->bOrientRotationToMovement = true;
	//在本地找到模型文件,并赋值给mesh设置相应参数
	static ConstructorHelpers::FObjectFinder<USkeletalMesh> playerPawn(TEXT("/Game/Animation/mon_goblinWizard/mon_goblinWizard"));
	GetMesh()->SetSkeletalMesh(playerPawn.Object);
	GetMesh()->SetRelativeLocation(FVector(0, 0, -95.f));
	GetMesh()->SetRelativeRotation(FRotator(0,-90.f,0.f));
	//创建一个摄像机目标点 USpringArmComponent 设置父级 长度 可旋转
	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	CameraBoom->AttachTo(RootComponent);
	CameraBoom->TargetArmLength = 300;
	CameraBoom->bUsePawnControlRotation = true;
	//创建一个摄像机 绑定到CameraBoom下,不受控制旋转的影响 受CameraBoom旋转影响
	FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	FollowCamera->AttachTo(CameraBoom, USpringArmComponent::SocketName);
	FollowCamera->bUsePawnControlRotation = false;
	
}

// Called when the game starts or when spawned
void AMyThirdCharacter::BeginPlay()
{
	Super::BeginPlay();
	//设置初始位置
	RootComponent->SetWorldLocation(FVector(0, 0, 0));
}

// Called every frame
void AMyThirdCharacter::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

// Called to bind functionality to input
//处理输入事件
void AMyThirdCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	Super::SetupPlayerInputComponent(InputComponent);
	InputComponent->BindAxis("MoveForward", this, &AMyThirdCharacter::MoveForward);
	InputComponent->BindAxis("MoveRight", this, &AMyThirdCharacter::MoveRight);
	InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
	InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
	
	InputComponent->BindAction("JumpBtn",IE_Pressed,this, &ACharacter::Jump);
	InputComponent->BindAction("JumpBtn", IE_Released,this, &ACharacter::StopJumping);
}

void AMyThirdCharacter::MoveForward(float value)
{
	if ((Controller != NULL) && (value != 0.0f))
	{
		// 找到当前的前方向
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);
		

		// 获取到前方向
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
		AddMovementInput(Direction, value);
	}
}

void AMyThirdCharacter::MoveRight(float value)
{
	if ((Controller != NULL) && (value != 0.0f))
	{
		// 找到当前的前方向
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0, Rotation.Yaw, 0);

		// 获取到右方向
		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
		AddMovementInput(Direction, value);
	}
}
5.      设置GameModeDefaultPawnClass,编译运行


二、蓝图创建

1.创建一个新的蓝图,继承自Character,取名MyThirdHero

2.双击打开蓝图,选择MyThirdHero,确定bUseControllerRotationPitchbUseControllerRotationRollbUseControllerRotationYaw均为false,不受旋转影响

3.选中mesh,在SkeletalMesh中,选择自己的导入的模型,并设置位置旋转等相应参数,可在Viewport中看见详细情况

4.选中CapsuleComponent,在Shape栏下,可以根据模型大小调整碰撞胶囊的大小、

5.创建一个摄像机目标点,点击AddComponent,在Camera分类中有一个Spring Arm,点击创建,选中在右边可以设置他的参数,比如TargetArm Length则是,他与摄像机的距离,这里主要还得勾选UsePawn Control Rotation,让他可以旋转

6.创建一个摄像机,让他作为SpringArm的子节点,确定摄像机的Use Pawn Control Rotation为不勾选状态,摄像机跟随父节点的旋转

7.选中CharacterMovement,确定OrientRotationtoMovement为勾选状态,让他可以在移动的时候旋转

8.开始编写输入控制,让角色可以动起来


9.编译,设置GameModeDefaultPawnClass,编译,运行

posted on 2016-01-07 20:59  王亮1  阅读(1128)  评论(0)    收藏  举报

导航