当前位置: 首页 > news >正文

UE5 游戏模板 —— ThirdPersonGame

UE5 游戏模板 —— ThirdPersonGame

  • 前言
  • 一、初始化
    • 旋转控制参数
      • 1.参数一
      • 2.参数二
      • 3.参数三
      • 4.参数四
  • 二、输入系统
  • 总结


前言

有了前面的铺垫,第三人称模板简直是手到擒来了,我们只需要注意一些初始化的变量是做什么的即可,因为UE的Character 提供的功能和 PawnMovementComponent 组件提供的功能足够我们做一些常见的角色控制了。

一、初始化

以下是初始化的代码,可以看到创建了相机的支架和相机,其余的角色网格以及胶囊体和移动组件都是在Character基类中初始化好了的

在这里插入图片描述

旋转控制参数

1.参数一

如下的三个参数分别是三个轴向是否使用Ctrl的旋转进行控制,很好理解即角色的旋转是否要和鼠标(输入)相互绑定,这样实现的效果就是角色将永远无法看到正脸。

	bUseControllerRotationPitch = false;bUseControllerRotationYaw = false;bUseControllerRotationRoll = false;

2.参数二

此参数主要是用于使角色总是身体始终面向移动方向,角色会转到当前方向。注意和 **bUseControllerRotationYaw ** 互斥。

	// Configure character movementGetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...	GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...at this rotation rate

3.参数三

在这里插入图片描述在这里插入图片描述

4.参数四

bUsePawnControlRotation 是组件是否跟随控制器旋转,由于我们是第三人称相机又在相机杆上所以只要相机杆跟随输入控制器旋转就行了,如果是第一人称游戏不需要相机杆只有一个相机相机跟随输入控制器旋转即可

二、输入系统

以及和之前的第一人称和俯视角的输入系统类似了,这里只简单贴一下代码

void ATestThirdPersonCharacter::NotifyControllerChanged()
{Super::NotifyControllerChanged();// Add Input Mapping Contextif (APlayerController* PlayerController = Cast<APlayerController>(Controller)){if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer())){Subsystem->AddMappingContext(DefaultMappingContext, 0);}}
}void ATestThirdPersonCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{// Set up action bindingsif (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent)) {// JumpingEnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &ACharacter::Jump);EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);// MovingEnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ATestThirdPersonCharacter::Move);// LookingEnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ATestThirdPersonCharacter::Look);}else{UE_LOG(LogTemplateCharacter, Error, TEXT("'%s' Failed to find an Enhanced Input component! This template is built to use the Enhanced Input system. If you intend to use the legacy system, then you will need to update this C++ file."), *GetNameSafe(this));}
}void ATestThirdPersonCharacter::Move(const FInputActionValue& Value)
{// input is a Vector2DFVector2D MovementVector = Value.Get<FVector2D>();if (Controller != nullptr){// find out which way is forwardconst FRotator Rotation = Controller->GetControlRotation();const FRotator YawRotation(0, Rotation.Yaw, 0);// get forward vectorconst FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);// get right vector const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);// add movement AddMovementInput(ForwardDirection, MovementVector.Y);AddMovementInput(RightDirection, MovementVector.X);}
}void ATestThirdPersonCharacter::Look(const FInputActionValue& Value)
{// input is a Vector2DFVector2D LookAxisVector = Value.Get<FVector2D>();if (Controller != nullptr){// add yaw and pitch input to controllerAddControllerYawInput(LookAxisVector.X);AddControllerPitchInput(LookAxisVector.Y);}
}

总结

此模板适用于快速搭建第三人称角色,强调输入响应和视角分离,后续可扩展动画或物理交互。

http://www.xdnf.cn/news/1065583.html

相关文章:

  • 深度解析云计算网络架构:VLAN+OVS+Bonding构建高可靠虚拟化平台
  • 给同一个wordpress网站绑定多个域名的实现方法
  • 人工智能、机器人最容易取哪些体力劳动和脑力劳动
  • 《计算机网络:自顶向下方法(第8版)》Chapter 8 课后题
  • 从零开始手写redis(16)实现渐进式 rehash map
  • (码云gitee)IDEA新项目自动创建gitee仓库并直接提交
  • 【Datawhale组队学习202506】YOLO-Master task03 IOU总结
  • 【51单片机】串口通信
  • 在windows上使用file命令
  • 2140、解决智力问题
  • 核心概念解析:AI、数据挖掘、机器学习与深度学习的关系
  • P99延迟:系统性能优化的关键指标
  • 管理综合知识点
  • Kafka与Zookeeper在linux上的下载记录
  • 【PyTorch项目实战】CycleGAN:无需成对训练样本,支持跨领域图像风格迁移
  • git 如何忽略某个文件夹文件
  • dijkstra(迪杰斯特拉)算法详解
  • React 核心原理与Fiber架构
  • Python 的内置函数 help
  • 【力扣 中等 C】983. 最低票价
  • Linux内核中安全创建套接字:为何inet_create未导出及正确替代方案
  • rust单体web项目模板搭建
  • JAVA集合篇--深入理解ConcurrentHashMap图解版
  • Dalvik和ART的区别
  • 华为云Flexus+DeepSeek征文|开启DeepSeek-V3+R1商用服务之旅
  • 顶顶通AI呼叫软件(大模型电话机器人)介绍
  • Flink源码阅读环境准备全攻略:搭建高效探索的基石
  • [论文阅读] 软件工程 + 教学 | 软件工程项目管理课程改革:从传统教学到以学生为中心的混合式学习实践
  • Spark教程6:Spark 底层执行原理详解
  • C++法则8:对于有引用成员的类,合成拷贝赋值运算符被定义为删除的。