1.创建输入动作:InputAction_Look
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Input", meta = (AllowPrivateAccess = "true"))
TObjectPtr<UInputAction> InputAction_Look;
2.创建HandleLookInput()回调函数
void HandleLookInput(const FInputActionValue& InputActionValue);
实现:
void ACPlayerCharacter::HandleLookInput(const FInputActionValue& InputActionValue)
{
//获取鼠标轴二维向量
const FVector2D LookAxisVector = InputActionValue.Get<FVector2D>();
//左右看
if (LookAxisVector.X != 0.f)
{
AddControllerYawInput(LookAxisVector.X);
}
// 上下看
if (LookAxisVector.Y != 0.f)
{
AddControllerPitchInput(LookAxisVector.Y);
}
}
3.绑定到映射情境:
EnhancedInputComponent->BindAction(
InputAction_Look,
ETriggerEvent::Triggered,
this,
&ACPlayerCharacter::HandleLookInput);
4.创建输入动作:
绑定到映射情境:
角色蓝图类配置资产:
5.左右可以旋转,但是上下不能动:
在Source/Crunch/Private/Player/CPlayerCharacter.cpp:构造函数:
CameraBoom->bUsePawnControlRotation = true; //使用棋子控制旋转
bUsePawnControlRotation
是UE中控制摄像机旋转行为的关键参数,当设置为true时表示弹簧臂组件将跟随Pawn控制器的旋转。该参数通常与角色移动组件和摄像机组件配合使用,形成完整的第三人称视角控制系统。
主要特性包括:
旋转控制
- 使弹簧臂的旋转受玩家控制器输入影响(如鼠标移动)
- 通常配合
bUseControllerRotationYaw/Pitch/Roll
使用以控制各轴向旋转
典型应用场景
- 第三人称角色摄像机系统
- 需要手动控制摄像机旋转角度的场景
- 与
FollowCamera->bUsePawnControlRotation=false
组合使用实现分离控制
实现原理
- 通过
APlayerController
的ControlRotation
驱动旋转 - 最终影响弹簧臂及其子组件(如摄像机)的世界变换
- 与碰撞检测协同工作时可实现摄像机避障
该参数的默认值通常为false,需要显式设置为true才能实现玩家输入控制摄像机旋转的效果。在
Lyra等高级摄像机系统中,该参数还会影响CameraMode的计算逻辑。
6.不让角色随着鼠标旋转:
/*让角色自己转到方向,不用鼠标旋转*/
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
这三个参数控制角色与控制器旋转的关联性,是UE中角色运动系统的核心配置项。具体作用如下:
参数功能
bUseControllerRotationPitch
:禁用控制器俯仰角(Pitch)影响角色旋转bUseControllerRotationYaw
:禁用控制器偏航角(Yaw)影响角色旋转bUseControllerRotationRoll
:禁用控制器滚转角(Roll)影响角色旋转
典型应用场景
- 第三人称角色需要独立控制摄像机与角色朝向时
- 配合
bOrientRotationToMovement=true
实现移动方向自动转向 - 需要分离摄像机控制与角色行为的ARPG/ACT游戏
系统关联性
- 需与
CameraBoom->bUsePawnControlRotation
配合使用 - 影响
CharacterMovementComponent
的旋转计算逻辑 - 与输入映射共同构成完整的控制系统
该配置常见于需要角色移动方向与输入方向保持一致的第三人称游戏,如《黑暗之魂》类角色控制方案。
GetCharacterMovement()->bOrientRotationToMovement = true; //保持旋转到位移方向
bOrientRotationToMovement
是UE中控制角色移动方向与旋转关系的核心参数,设置为true时角色会自动转向移动方向。该参数通常与控制器旋转参数配合使用,形成完整的角色控制系统。
主要特性包括:
行为表现
- 角色会根据移动输入方向自动调整朝向
- 旋转速度由
RotationRate
参数控制 - 适用于RPG/ACT类游戏的移动模式
典型配置组合
- 需配合
bUseControllerRotationYaw=false
禁用控制器旋转影响 - 常与
SpringArm->bUsePawnControlRotation=true
组合实现第三人称摄像机 - 移动输入需基于摄像机坐标系转换
注意事项
- 直接使用可能导致角色持续旋转(如侧移时)
- 复杂移动逻辑需配合自定义输入向量计算
- 网络同步时需特殊处理旋转插值
该模式常见于《黑暗之魂》等第三人称游戏,提供更自然的移动转向体验。实际开发中需根据游戏类型调整RotationRate
等参数优化操作手感。
源代码:
Source/Crunch/Public/Player/CPlayerCharacter.h:
// Copyright@ChenChao
#pragma once
#include "CoreMinimal.h"
#include "Character/CCharacter.h"
#include "CPlayerCharacter.generated.h"
struct FInputActionValue;
class UInputAction;
class UInputMappingContext;
class USpringArmComponent;
class UCameraComponent;
/**
*
*/
UCLASS()
class CRUNCH_API ACPlayerCharacter : public ACCharacter
{
GENERATED_BODY()
public:
ACPlayerCharacter();
//客户端首次生成实际角色调用
virtual void PawnClientRestart() override;
//设置玩家输入组件
virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;
private:
#pragma region Components
//弹簧臂组件
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))
TObjectPtr<USpringArmComponent> CameraBoom;
//跟随摄像机组件
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))
TObjectPtr<UCameraComponent> FollowCamera;
#pragma endregion
#pragma region Input
//输入映射上下文
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Input", meta = (AllowPrivateAccess = "true"))
TObjectPtr<UInputMappingContext> InputContext;
//输入动作
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Input", meta = (AllowPrivateAccess = "true"))
TObjectPtr<UInputAction> InputAction_Jump;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Input", meta = (AllowPrivateAccess = "true"))
TObjectPtr<UInputAction> InputAction_Look;
//输入动作回调函数
void HandleLookInput(const FInputActionValue& InputActionValue);
#pragma endregion
};
Source/Crunch/Private/Player/CPlayerCharacter.cpp:
// Copyright@ChenChao
#include "Player/CPlayerCharacter.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"
ACPlayerCharacter::ACPlayerCharacter()
{
/*让角色自己转到方向,不用鼠标旋转*/
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
/*弹簧臂组件*/
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(GetRootComponent()); //设置父级(根组件)
CameraBoom->TargetArmLength = 800.f; //设置臂长
CameraBoom->SocketOffset = FVector(0.f, 55.f, 65.f); //插槽偏移
CameraBoom->bUsePawnControlRotation = true; //使用棋子控制旋转
/*相机组件*/
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); //设置父级(弹簧臂插槽名称)
FollowCamera->bUsePawnControlRotation = false;
/*角色移动组件*/
GetCharacterMovement()->bOrientRotationToMovement = true; //保持旋转到位移方向
GetCharacterMovement()->RotationRate = FRotator(0.f, 500.f, 0.f); //旋转速度(转身速度)
GetCharacterMovement()->MaxWalkSpeed = 400.f; //最大位移速度
GetCharacterMovement()->BrakingDecelerationWalking = 200.f; //行走加速度
}
void ACPlayerCharacter::PawnClientRestart()
{
Super::PawnClientRestart();
// 获取玩家控制器
APlayerController* OwningPlayerController = Cast<APlayerController>(GetController());
if (OwningPlayerController)
{
//获取本地玩家子系统
class ULocalPlayer* LocalPlayer = OwningPlayerController->GetLocalPlayer();
//获取本地玩家子系统
UEnhancedInputLocalPlayerSubsystem* Subsystem = LocalPlayer->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>();
if (Subsystem)
{
checkf(InputContext, TEXT("InputContext未配置资产!"));
//先移除旧的输入上下文
Subsystem->RemoveMappingContext(InputContext);
//绑定映射情境
Subsystem->AddMappingContext(InputContext, 0);
}
}
}
void ACPlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
//增强输入组件
UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent);
if (EnhancedInputComponent)
{
checkf(InputAction_Jump, TEXT("InputAction_Jump未配置资产!"));
checkf(InputAction_Look, TEXT("InputAction_Look未配置资产!"));
//动作绑定
EnhancedInputComponent->BindAction(InputAction_Jump, ETriggerEvent::Triggered, this, &ACPlayerCharacter::Jump);
EnhancedInputComponent->BindAction(InputAction_Look, ETriggerEvent::Triggered, this, &ACPlayerCharacter::HandleLookInput);
}
}
void ACPlayerCharacter::HandleLookInput(const FInputActionValue& InputActionValue)
{
//获取鼠标轴二维向量
const FVector2D LookAxisVector = InputActionValue.Get<FVector2D>();
//左右看
if (LookAxisVector.X != 0.f)
{
AddControllerYawInput(LookAxisVector.X);
}
// 上下看
if (LookAxisVector.Y != 0.f)
{
AddControllerPitchInput(LookAxisVector.Y);
}
}