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);}
}
总结
此模板适用于快速搭建第三人称角色,强调输入响应和视角分离,后续可扩展动画或物理交互。