13-Character类的基本使用示例 UE4 C++

发布于:2022-10-12 ⋅ 阅读:(807) ⋅ 点赞:(0)

效果:

详细步骤:

1.首先在EpicGames中的虚幻商城搜索“太阳神庙”,然后下载并创建工程

2.在库中打开太阳神庙工程

 

 我想要把上图中的这个场景放到之前学习的工程中去

1.在 内容 中找到 Maps,然后右键选择迁移

 2.点击确定

3.选择迁移到的路径

 4.现在可以关闭太阳神庙这个工程,然后打开之前学习的工程

5.打开Maps文件夹

 双击关卡 来切换为这个关卡

 6.现在可以设置默认关卡为这个关卡

打开编辑-》项目设置-》 项目-》地图和模式,然后设置编辑器开始地图和游戏默认地图为SunTemple

 7.我们可以用之前的MyPawn来游玩下这个场景

在世界场景设置中,将默认pawn类改为BP_MyPawn 

 

 此时运行游戏就可以控制之前的Pawn在这个关卡中移动:

8. 如果要换个角色,可以先到虚幻商城中下载,我下载的是这个:

 然后添加到工程

 添加后就可以看到这个文件夹:

 9.下面开始创建C++ Character类

 

选择继承角色类: 

 命名为Man,然后创建类

 在Man.h中添加如下代码:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Man.generated.h"

UCLASS()
class CTESTPROJECT_API AMan : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AMan();

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
	class USpringArmComponent* SpringArmComp;  //定义一个镜头摇臂

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
	class UCameraComponent* Camera;  //定义一个镜头

	float BaseTurnRate;  //定义旋转率,控制向左右转的速度
	float BaseLookupRate;  //控制向上下看的速度
	void MoveForward(float Value);
	void MoveRight(float Value);
	void TurnAtRate(float Value);  
	void TurnLookupRate(float Value);


protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

};

 在Man.cpp中添加如下代码:

// Fill out your copyright notice in the Description page of Project Settings.


#include "Man.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/Controller.h"
#include "Engine/World.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"


// Sets default values
AMan::AMan()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
	SpringArmComp->SetupAttachment(RootComponent);
	SpringArmComp->TargetArmLength = 600;
	SpringArmComp->bUsePawnControlRotation = true;  //摇臂跟着控制器旋转

	Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
	Camera->SetupAttachment(SpringArmComp, USpringArmComponent::SocketName);
	Camera->bUsePawnControlRotation = false;  //镜头不用跟着摇臂转,因为镜头长在摇臂上

	BaseTurnRate = 65;
	BaseLookupRate = 65;

	//设置模型不跟着控制器旋转,因为我们只要控制镜头转就行了
	bUseControllerRotationYaw = false;  
	bUseControllerRotationPitch = false;
	bUseControllerRotationRoll = false;

	GetCharacterMovement()->bOrientRotationToMovement = true;  //当开始移动时,角色会朝着移动方向转身
	GetCharacterMovement()->RotationRate = FRotator(0.f, 540, 0);  //控制转身的速度
	GetCharacterMovement()->JumpZVelocity = 650;  //角色跳的时候向上的初始速度
	GetCharacterMovement()->AirControl = 0.2f;  //腾空后玩家对角色横向运动的控制量

}

void AMan::MoveForward(float Value)  //前后移动的方法
{
	if (Controller && Value != 0)
	{
		const FRotator Rotation = Controller->GetControlRotation();  //拿到控制器的旋转
		const FRotator YawRotation(0.f, Rotation.Yaw, 0);  //作为左右旋转的值

		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);  //构造旋转矩阵,从矩阵中获取x轴的向量
		AddMovementInput(Direction, Value);
	}
}

void AMan::MoveRight(float Value)  //左右移动的方法
{
	if (Controller && Value != 0)
	{
		const FRotator Rotation = Controller->GetControlRotation();
		const FRotator YawRotation(0.f, Rotation.Yaw, 0);

		const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
		AddMovementInput(Direction, Value);
	}
}

void AMan::TurnAtRate(float Value)  //左右看的方法
{
	AddControllerYawInput(Value* BaseTurnRate* GetWorld()->DeltaTimeSeconds);
}

void AMan::TurnLookupRate(float Value)  //上下看的方法
{
	AddControllerPitchInput(Value* BaseTurnRate* GetWorld()->DeltaTimeSeconds);
}

// Called when the game starts or when spawned
void AMan::BeginPlay()
{
	Super::BeginPlay();
	
}

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

}

// Called to bind functionality to input
void AMan::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
	PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);

	PlayerInputComponent->BindAxis("MoveForward", this, &AMan::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &AMan::MoveRight);

	PlayerInputComponent->BindAxis("CameraPitch", this, &APawn::AddControllerPitchInput);
	PlayerInputComponent->BindAxis("CameraYaw", this, &APawn::AddControllerYawInput);

	PlayerInputComponent->BindAxis("Lookup", this, &AMan::TurnLookupRate);
	PlayerInputComponent->BindAxis("Turn", this, &AMan::TurnAtRate);
}

 10.在项目设置-》引擎-》输入 进行如下设置:

 11.新建一个蓝图取继承Man这个C++类

命名为MyMan 

 

 双击打开这个蓝图:

12.在Mesh组件中选择一个怪物的骨架网格体:

 13.调整胶囊体的高度和半径,让其正好能够包裹人物模型(注意:一定要让人物模型的正方向与胶囊体的正方向保持一致

 14.在世界场景设置中把默认的pawn类设为MyMan这个蓝图

 

然后就可以看到开头的效果了

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到