栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

unreal-c++教程-第六章:控制你的角色

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

unreal-c++教程-第六章:控制你的角色

控制你的角色
    • 效果演示
    • 1.1 创建控制类
    • 1.2 通过GameMode生成角色
      • 1.2.1 Unreal的GamePlay框架
      • 1.2.2 设置GamePlay
    • 1.3 c++角色添加摄像机
      • 1.3.1 鼠标控制SpringArm
    • 1.4 移动控制

效果演示

link

我们在第五章已经成功的创建了你的角色,我们现在希望去控制它。
对此。为了便于管理,我们会将控制类整理到一个类中。
1.1 创建控制类

AInputManager.cpp

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


#include "InputManager.h"

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

}

// Called when the game starts or when spawned
void AInputManager::BeginPlay()
{
	Super::BeginPlay();
	
	auto world = this->GetWorld();
	auto playerController= world->GetFirstPlayerController();
	auto inputComp = playerController->InputComponent;

	inputComp->BindAxis("MouseTurnInput", this, &AInputManager::MouseTurnInput);
	inputComp->BindAxis("MouseTurnUpInput", this, &AInputManager::MouseTurnUpInput);
	inputComp->BindAxis("RightDirInput", this, &AInputManager::RightDirInput);
	inputComp->BindAxis("ForwardDirInput", this, &AInputManager::ForawrdDirInput);

}

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

}

void AInputManager::MouseTurnInput(float val)
{

}

void AInputManager::MouseTurnUpInput(float val)
{

}

void AInputManager::ForawrdDirInput(float val)
{

}

void AInputManager::RightDirInput(float val)
{

}


1.2 通过GameMode生成角色 1.2.1 Unreal的GamePlay框架
在Unreal中,定义游戏的基本规则是通过GameMode来完成的。
本章我们重点关注的是GameMode会处理玩家的生成。
1.2.2 设置GamePlay
在我们创建一个第三人称项目的时候,项目会默认带着一个GamePlay的配置脚本


我们打开这个GamePlay配置脚本,会发现有很多配置项
本章我们关注的是通过GamePlay 控制默认生成的对象类为我们的BaseRole



注意编译(再强调一下)
另外一个问题,当我们有很多GamePlay的配置脚本时,我们应该怎么去选择应用某个GamePlay?


然后我们再将场景中的第三人称的角色删掉

当我们再度运行游戏的时候,它会给你生成一个默认的BaseRole


这里其实存在几个问题

  1. 我们其实希望我们的默认角色是一个已经放在场景中的角色

    这个时候,我们可以通过指定该角色为 PlayerController 默认拥有的角色0 即可

  2. 缺少相机

这时候我们看到的画面依旧是非常鬼畜的,因为我们缺少相机

我们需要针对这个解决一下

1.3 c++角色添加摄像机

核心代码:

ABaseRole::ABaseRole()
{
 	// 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;
	auto mesh = this->GetMesh();
	auto asset = ConstructorHelpers::FObjectFinder(TEXT("SkeletalMesh'/Game/Demos/Mesh/BaseRole/BaseRole.BaseRole'"));
	mesh->SetSkeletalMesh(asset.Object);
	mesh->SetRelativeLocation(FVector(0, 0, -80));
	mesh->SetRelativeRotation(FRotator(0,-90, 0));
	ConstructorHelpers::FClassFinder meshAnima(TEXT("/Game/Demos/Animator/AnimBP/BaseRoleAnimBP"));
	auto animClass = meshAnima.Class;
	mesh->SetAnimClass(animClass);
	bUseControllerRotationYaw = false;
	camera = CreateDefaultSubobject(TEXT("Camera"));
	arm = CreateDefaultSubobject(TEXT("Arm"));
	arm->AttachTo(RootComponent);
	arm->TargetArmLength = 300.f;
	arm->SetRelativeRotation(FRotator(-45, 0, 0));
	arm->bEnableCameraLag = true;
	arm->CameraLagSpeed = 5;
	arm->CameraLagMaxDistance = 1.5f;
	arm->bEnableCameraRotationLag = true;
	arm->CameraRotationLagSpeed = 10;
	arm->CameraLagMaxTimeStep = 1;
	camera->AttachTo(arm,USpringArmComponent::SocketName);

}

这里面关于视角的控制函数:

void ABaseRole::VerticalRot(float val)
{
	if (val) {
		float temp = arm->GetRelativeRotation().Pitch + val;
		if (temp < 25 && temp > -65) {
			arm->AddLocalRotation(FRotator(val, 0, 0));
		}
	}
}


void ABaseRole::HorizontalRot(float val)
{
	if (val) {
		AddActorLocalRotation(FRotator(0, val, 0));
	}
}

1.3.1 鼠标控制SpringArm
一般,我们的控制逻辑都会在一个控制类中实现,也就是InputManager去处理我们的所有控制逻辑
所以当前我们要解决的问题就是:怎么在InputManager中获取这个游戏对象
一般由两种办法:
一:直接从PlayerController中获取
二:当我们创建该对象时,可以将该对象存放成一个共享变量,然后通过共享变量去访问

从PlayerController中获取Playter0


在InputManager里面,当我们接收到鼠标移动的信号时,直接call role的两个视角旋转函数即可

void AInputManager::MouseTurnInput(float val)
{
	//FString TheFloatStr = FString::SanitizeFloat(val);
	role->HorizontalRot(val);
}

void AInputManager::MouseTurnUpInput(float val)
{   
	role->VerticalRot(val);
}
1.4 移动控制

代码也比较简单:

void ABaseRole::RightMove(float val)
{
	FRotator CurrentRotation = this->GetActorRotation();
	const FVector Direction = FRotationMatrix(CurrentRotation).GetScaledAxis(EAxis::Y);
	AddMovementInput(Direction, val * 10);
}

void ABaseRole::ForwardMove(float val)
{
	FRotator CurrentRotation = this->GetActorRotation();
	const FVector Direction = FRotationMatrix(CurrentRotation).GetScaledAxis(EAxis::X);
	AddMovementInput(Direction, val * 10);
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985614.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号