找回密码
 立即注册
查看: 318|回复: 0

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

[复制链接]
发表于 2023-4-12 22:14 | 显示全部楼层 |阅读模式
控制你的角色


      效果演示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. 我们在第五章已经成功的创建了你的角色,我们现在希望去控制它。
  2. 对此。为了便于管理,我们会将控制类整理到一个类中。
复制代码
1.1 创建控制类

AInputManager.cpp
  1. // Fill out your copyright notice in the Description page of Project Settings.#include"InputManager.h"// Sets default values
  2. AInputManager::AInputManager(){// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  3.         PrimaryActorTick.bCanEverTick = true;}// Called when the game starts or when spawnedvoid AInputManager::BeginPlay(){
  4.         Super::BeginPlay();auto world = this->GetWorld();auto playerController= world->GetFirstPlayerController();auto inputComp = playerController->InputComponent;
  5.         inputComp->BindAxis("MouseTurnInput", this,&AInputManager::MouseTurnInput);
  6.         inputComp->BindAxis("MouseTurnUpInput", this,&AInputManager::MouseTurnUpInput);
  7.         inputComp->BindAxis("RightDirInput", this,&AInputManager::RightDirInput);
  8.         inputComp->BindAxis("ForwardDirInput", this,&AInputManager::ForawrdDirInput);}// Called every framevoid AInputManager::Tick(float DeltaTime){
  9.         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框架
  1. 在Unreal中,定义游戏的基本规则是通过GameMode来完成的。
  2. 本章我们重点关注的是GameMode会处理玩家的生成。
复制代码
1.2.2 设置GamePlay
  1. 在我们创建一个第三人称项目的时候,项目会默认带着一个GamePlay的配置脚本
复制代码

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


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

然后我们再将场景中的第三人称的角色删掉
当我们再度运行游戏的时候,它会给你生成一个默认的BaseRole

这里其实存在几个问题
    我们其实希望我们的默认角色是一个已经放在场景中的角色


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



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


我们需要针对这个解决一下
1.3 c++角色添加摄像机

核心代码:
  1. ABaseRole::ABaseRole(){// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  2.         PrimaryActorTick.bCanEverTick = true;auto mesh = this->GetMesh();auto asset = ConstructorHelpers::FObjectFinder<USkeletalMesh>(TEXT("SkeletalMesh'/Game/Demos/Mesh/BaseRole/BaseRole.BaseRole'"));
  3.         mesh->SetSkeletalMesh(asset.Object);
  4.         mesh->SetRelativeLocation(FVector(0,0,-80));
  5.         mesh->SetRelativeRotation(FRotator(0,-90,0));
  6.         ConstructorHelpers::FClassFinder<UAnimInstance>meshAnima(TEXT("/Game/Demos/Animator/AnimBP/BaseRoleAnimBP"));auto animClass = meshAnima.Class;
  7.         mesh->SetAnimClass(animClass);
  8.         bUseControllerRotationYaw = false;
  9.         camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
  10.         arm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Arm"));
  11.         arm->AttachTo(RootComponent);
  12.         arm->TargetArmLength =300.f;
  13.         arm->SetRelativeRotation(FRotator(-45,0,0));
  14.         arm->bEnableCameraLag = true;
  15.         arm->CameraLagSpeed =5;
  16.         arm->CameraLagMaxDistance =1.5f;
  17.         arm->bEnableCameraRotationLag = true;
  18.         arm->CameraRotationLagSpeed =10;
  19.         arm->CameraLagMaxTimeStep =1;
  20.         camera->AttachTo(arm,USpringArmComponent::SocketName);}
复制代码
这里面关于视角的控制函数:
  1. void ABaseRole::VerticalRot(float val){if(val){float temp = arm->GetRelativeRotation().Pitch + val;if(temp <25&& temp >-65){
  2.                         arm->AddLocalRotation(FRotator(val,0,0));}}}void ABaseRole::HorizontalRot(float val){if(val){AddActorLocalRotation(FRotator(0, val,0));}}
复制代码


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

在InputManager里面,当我们接收到鼠标移动的信号时,直接call role的两个视角旋转函数即可
  1. void AInputManager::MouseTurnInput(float val){//FString TheFloatStr = FString::SanitizeFloat(val);
  2.         role->HorizontalRot(val);}void AInputManager::MouseTurnUpInput(float val){   
  3.         role->VerticalRot(val);}
复制代码
1.4 移动控制

代码也比较简单:
  1. void ABaseRole::RightMove(float val){
  2.         FRotator CurrentRotation = this->GetActorRotation();const FVector Direction =FRotationMatrix(CurrentRotation).GetScaledAxis(EAxis::Y);AddMovementInput(Direction, val *10);}void ABaseRole::ForwardMove(float val){
  3.         FRotator CurrentRotation = this->GetActorRotation();const FVector Direction =FRotationMatrix(CurrentRotation).GetScaledAxis(EAxis::X);AddMovementInput(Direction, val *10);}
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-16 16:04 , Processed in 0.089478 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表