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

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

首先,我们将设置为W,A,S,D键轴映射。

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

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

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

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

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



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

7.在第二个下啦菜单,输入 -1如下图

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);
        }
    }
好了,变异运行就能看到你的角色移动了
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值