我的虚幻4之旅03 添加按键事件

          好久不更新博客,最近比较忙没时间更新,年前估计也只能断断续续的更新。这节俺想讨论下虚幻四里按键触发。

首先,我们将设置为WA,SD键轴映射

1.在编辑菜单上的项目设置单击

2.引擎的标题的项目设置标签的左侧点击输入

3.在绑定请单击加号旁边映射

4.“MoveForward出现的文本字段然后单击箭头以文本框的扩大结合选项

5.在下拉菜单中,选择 w输入设置现在看起来应该像下面


我的虚幻4之旅03 添加按键事件_第1张图片

6.现在,点击+号 添加下一个MoveForward

7.在第二个下啦菜单,输入 -1如下图我的虚幻4之旅03 添加按键事件_第2张图片

8.关闭设置界面

9.进入vs or xcode 在FPSCharacter。h里添加

    protected:
        virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
    //handles moving forward/backward
    UFUNCTION()
    void MoveForward(float Val);
    //handles strafing
    UFUNCTION()
    void MoveRight(float Val);
10 在cpp里添加

  void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* InputComponent)
    {
        // set up gameplay key bindings
        InputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
        InputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
    }
    void AFPSCharacter::MoveForward(float Value)
    {
        if ( (Controller != NULL) && (Value != 0.0f) )
        {
            // find out which way is forward
            FRotator Rotation = Controller->GetControlRotation();
            // Limit pitch when walking or falling
            if ( CharacterMovement->IsMovingOnGround() || CharacterMovement->IsFalling() )
            {
                Rotation.Pitch = 0.0f;
            }
            // add movement in that direction
            const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
            AddMovementInput(Direction, Value);
        }
    }


    void AFPSCharacter::MoveRight(float Value)
    {
        if ( (Controller != NULL) && (Value != 0.0f) )
        {
            // find out which way is right
            const FRotator Rotation = Controller->GetControlRotation();
            const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);
            // add movement in that direction
            AddMovementInput(Direction, Value);
        }
    }
好了,变异运行就能看到你的角色移动了

你可能感兴趣的:(unreal,engine,4)