APawn::AddMovementInput 简介

博客介绍了UE4里AddMovementInput相关内容,包含其语法、参数,并给出示例代码,为开发者在UE4开发中使用AddMovementInput提供了技术参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.语法

virtual void AddMovementInput
(
    FVector WorldDirection,
    float ScaleValue,
    bool bForce
)

2.参数

参数描述
WorldDirection使用世界方向向量表示移动方向
ScaleValue通常使用鼠标位移表示
bForce如果为真,表示总是使用此输入,忽略IsMoveInputIgnored()作用

 

 

 

 

 

  

 

 

3.示例代码

if (Value != 0.0f)  
    {  
        // Add movement in the actors forward direction. Use the axis scale value as the scale of movement to add  
        AddMovementInput(GetActorForwardVector(), Value);  
    }  

 

<think>我们正在使用Unreal Engine和C++创建一个角色类。角色类通常继承自ACharacter类,它已经包含了角色移动组件(如CharacterMovementComponent)和骨骼网格体等基本功能。 步骤: 1. 在Unreal Editor中,我们可以通过创建C++类来开始。 2. 选择父类为Character(通常位于All Classes中,或者直接搜索)。 3. 命名我们的新类(例如MyCharacter)。 4. 然后,我们可以在生成的代码中扩展功能。 注意:在Unreal Engine中,使用C++创建类时,会使用一些特定的宏(如UCLASS())以及Unreal Header Tool(UHT)所需的标记。 下面是一个基本的角色类创建示例: 在头文件(MyCharacter.h)中: ```cpp #pragma once #include "CoreMinimal.h" #include "GameFramework/Character.h" #include "MyCharacter.generated.h" // 必须包含生成的.h文件 UCLASS() class MYPROJECT_API AMyCharacter : public ACharacter { GENERATED_BODY() public: // 设置默认值 AMyCharacter(); protected: // 游戏开始或生成时调用 virtual void BeginPlay() override; public: // 每帧调用 virtual void Tick(float DeltaTime) override; // 绑定输入功能 virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; }; ``` 在源文件(MyCharacter.cpp)中: ```cpp #include "MyCharacter.h" // 构造函数,设置默认值 AMyCharacter::AMyCharacter() { // 设置每帧都调用Tick函数 PrimaryActorTick.bCanEverTick = true; // 初始化摄像机 CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom")); CameraBoom->SetupAttachment(RootComponent); CameraBoom->TargetArmLength = 300.0f; // 摄像机到角色的距离 CameraBoom->bUsePawnControlRotation = true; // 使用pawn控制旋转 // 创建跟随摄像机 FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera")); FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // 附加到弹簧臂 FollowCamera->bUsePawnControlRotation = false; // 摄像机不旋转,让弹簧臂旋转 } // 游戏开始 void AMyCharacter::BeginPlay() { Super::BeginPlay(); } // 每帧调用 void AMyCharacter::Tick(float DeltaTime) { Super::Tick(DeltaTime); } // 绑定输入 void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); // 绑定移动 PlayerInputComponent->BindAxis("MoveForward", this, &AMyCharacter::MoveForward); PlayerInputComponent->BindAxis("MoveRight", this, &AMyCharacter::MoveRight); // 绑定视角 PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput); PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput); } // 自定义移动函数 void AMyCharacter::MoveForward(float Value) { if ((Controller != nullptr) && (Value != 0.0f)) { // 获取前向向量 const FRotator Rotation = Controller->GetControlRotation(); const FRotator YawRotation(0, Rotation.Yaw, 0); // 计算前向方向 const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X); AddMovementInput(Direction, Value); } } void AMyCharacter::MoveRight(float Value) { if ((Controller != nullptr) && (Value != 0.0f)) { // 获取右向量 const FRotator Rotation = Controller->GetControlRotation(); const FRotator YawRotation(0, Rotation.Yaw, 0); // 计算右向方向 const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y); AddMovementInput(Direction, Value); } } ``` 注意:上面的代码中,我们添加了一个弹簧臂(SpringArm)和一个摄像机(Camera),这是常见的第三人称视角设置。如果是第一人称,则不需要弹簧臂,而是将摄像机直接附加到角色的头部位置。 另外,我们绑定了输入轴(MoveForward, MoveRight, Turn, LookUp),这些输入轴需要在项目设置中定义(Edit->Project Settings->Input)。 在项目设置中,我们需要添加轴映射(Axis Mappings): - MoveForward: 通常绑定W(正向)和S(负向) - MoveRight: 通常绑定A(负向)和D(正向) - Turn: 鼠标X轴 - LookUp: 鼠标Y轴 创建完C++类后,我们需要在编辑器中创建一个蓝图类继承自这个C++类,以便于设置模型、动画等。 以上步骤创建了一个基本的可移动角色。当然,我们还可以添加更多功能,比如跳跃(可以使用ACharacter自带的跳跃功能,只需要在SetupPlayerInputComponent中绑定跳跃动作)、射击、交互等。 引用说明: 根据提供的引用,我们知道在Unreal Engine中使用C++创建自定义角色是通过扩展ACharacter类来实现的,并且可以利用UE提供的丰富功能[^1][^2]。同时,引用中也提到了C++在UE中的基础用法[^3]。 因此,我们按照上述步骤即可创建一个角色类。</think>在 Unreal Engine 中使用 C++ 创建角色类需要继承引擎的 `ACharacter` 基类,并实现必要的功能。以下是详细步骤和代码示例: ### 步骤 1:创建角色类头文件(.h) ```cpp // MyCharacter.h #pragma once #include "CoreMinimal.h" #include "GameFramework/Character.h" #include "MyCharacter.generated.h" // 必须包含 UCLASS() class MYPROJECT_API AMyCharacter : public ACharacter { GENERATED_BODY() public: // 构造函数 AMyCharacter(); protected: // 游戏开始时调用 virtual void BeginPlay() override; // 绑定输入 virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; // 移动函数 void MoveForward(float Value); void MoveRight(float Value); void JumpAction(); // 组件声明 UPROPERTY(VisibleAnywhere, Category = "Camera") class USpringArmComponent* SpringArm; UPROPERTY(VisibleAnywhere, Category = "Camera") class UCameraComponent* FollowCamera; }; ``` ### 步骤 2:实现角色类源文件(.cpp) ```cpp // MyCharacter.cpp #include "MyCharacter.h" #include "Camera/CameraComponent.h" #include "GameFramework/SpringArmComponent.h" AMyCharacter::AMyCharacter() { // 设置角色每帧调用Tick() PrimaryActorTick.bCanEverTick = true; // 创建弹簧臂组件 SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm")); SpringArm->SetupAttachment(RootComponent); SpringArm->TargetArmLength = 300.f; SpringArm->bUsePawnControlRotation = true; // 允许鼠标控制旋转 // 创建摄像机组件 FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera")); FollowCamera->SetupAttachment(SpringArm, USpringArmComponent::SocketName); FollowCamera->bUsePawnControlRotation = false; // 摄像机不随鼠标旋转 } void AMyCharacter::BeginPlay() { Super::BeginPlay(); } void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent); // 绑定移动输入 PlayerInputComponent->BindAxis("MoveForward", this, &AMyCharacter::MoveForward); PlayerInputComponent->BindAxis("MoveRight", this, &AMyCharacter::MoveRight); // 绑定鼠标控制 PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput); PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput); // 绑定跳跃 PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AMyCharacter::JumpAction); } void AMyCharacter::MoveForward(float Value) { if (Controller && Value != 0.f) { const FRotator ControlRotation = GetControlRotation(); const FRotator YawRotation(0, ControlRotation.Yaw, 0); const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X); AddMovementInput(Direction, Value); } } void AMyCharacter::MoveRight(float Value) { if (Controller && Value != 0.f) { const FRotator ControlRotation = GetControlRotation(); const FRotator YawRotation(0, ControlRotation.Yaw, 0); const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y); AddMovementInput(Direction, Value); } } void AMyCharacter::JumpAction() { Jump(); } ``` ### 关键点说明 1. **继承关系**: - 必须继承自 `ACharacter` 类 - 使用 `GENERATED_BODY()` 宏处理反射系统 - 类名前缀 `A` 表示 Actor 类型(如 `AMyCharacter`) 2. **组件系统**: - 使用 `CreateDefaultSubobject` 创建组件 - `UPROPERTY()` 宏使组件在编辑器中可见 - 弹簧臂(SpringArm)提供摄像机碰撞检测 3. **输入绑定**: - 在 `SetupPlayerInputComponent` 中绑定输入事件 - 轴映射(MoveForward/MoveRight)需在项目设置中定义 - 直接使用 `ACharacter` 内置的 `Jump()` 方法 4. **移动逻辑**: - 通过控制器旋转计算移动方向 - `AddMovementInput` 处理实际位移 - 内置 `CharacterMovementComponent` 自动处理物理碰撞 ### 后续步骤 1. 在编辑器创建蓝图子类: - 右键点击内容浏览器 → 蓝图类 → 选择 `MyCharacter` 为父类 2. 添加角色模型: - 在蓝图编辑器中设置骨骼网格体(Skeletal Mesh) 3. 配置输入映射: - Edit → Project Settings → Input - 添加轴映射(如 MoveForward, MoveRight 等) > 提示:使用 `ACharacter` 而非 `APawn` 可直接获得角色移动组件(CharacterMovementComponent)和胶囊体碰撞等预设功能[^1][^2]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值