• 虚幻引擎5:增强输入的使用方法


    一、包引入 

    项目中的build.cs文件中引入包

    二、基本配置 

    1.创建一个输入映射上下文(映射表)

    2.创建自己需要的操作映射或者轴映射

    3.创建完成之后进入这个映射,来设置类型,共有4个类型 

    1.Digital:是旧版操作映射类型,一般是按下抬起来使用,像跳跃,之类的

    2.剩余三个都是轴映射类型,Axis1D:单向轴,一般是油门按键,摇杆之类的

    3.Axis2D:有x,y两个方向,2D轴,一般是移动之类的

    4.Axis3D:有x,y,z三个方向

    4.将设置的映射添加至上下文中

    1.操作映射的配置就添加个按键直接使用就好了

    2.轴映射的配置

    5.然后设置对应的按键。基本设置就完成了

    三、使用方法 

    然后我这边选择使用C++来使用

    1.在使用此配置的角色类中引入按键映射mapping

    1. //头文件代码
    2. UPROPERTY(EditAnywhere)//此标记可以在蓝图中导入映射资产
    3. class UInputMappingContext*MappingContext;
    4. UPROPERTY(EditAnywhere)
    5. class UInputAction * JumpAction;//跳跃事件的action
    6. UPROPERTY(EditAnywhere)
    7. UInputAction * MoveAction;//移动事件的action
    8. virtual void BeginPlay() override;
    9. virtual void ALGPlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
    10. //轴映射移动绑定的函数,必须带有一下参数格式必须一致
    11. void Move(const FInputActionValue& Value);
    12. //cpp文件代码
    13. //按键操作一般在beginplay中执行
    14. void ALGPlayerCharacter::BeginPlay()
    15. {
    16. Super::BeginPlay();
    17. //判断一下资产是否添加,否则UE会崩溃
    18. if(MappingContext)
    19. {
    20. //因为按键是在角色身上执行的,所以需要找到角色控制器
    21. if (APlayerController* PC = Cast(GetController()))
    22. {
    23. //通过角色控制找到增强输入子系统
    24. UEnhancedInputLocalPlayerSubsystem*Subsystem=ULocalPlayer::GetSubsystem(PC->GetLocalPlayer());
    25. //将映射表添加至系统中
    26. Subsystem->AddMappingContext(MappingContext,0);
    27. }
    28. }
    29. }
    30. void ALGPlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
    31. {
    32. Super::SetupPlayerInputComponent(PlayerInputComponent);
    33. //使用了增强输入后,旧的输入控件就无法使用,必须使用新的
    34. if(UEnhancedInputComponent*EnhancedInputComponent = Cast(PlayerInputComponent))
    35. {
    36. //跳跃事件的绑定
    37. EnhancedInputComponent->BindAction(JumpAction,ETriggerEvent::Started,this,&ALGPlayerCharacter::DoJump);
    38. //移动事件的绑定
    39. EnhancedInputComponent->BindAction(MoveAction,ETriggerEvent::Triggered,this,&ALGPlayerCharacter::Move);
    40. }
    41. }
    42. //绑定的移动事件
    43. void ALGPlayerCharacter::Move(const FInputActionValue& Value)
    44. {
    45. 获取绑定事件给出的轴值
    46. FVector2D InputValue = Value.Get();
    47. UE_LOG(LogTemp, Log, TEXT("ok%s"),*InputValue.ToString())
    48. //前后移动
    49. AddMovementInput(GetActorForwardVector(), InputValue.Y);
    50. //左右移动
    51. AddMovementInput(GetActorRightVector(), InputValue.X);
    52. }

  • 相关阅读:
    maven本地仓库同步上传到nexus远程仓库
    数学学习基本理念与方法
    C++11之基础篇
    java-python+vue社区防疫服务管理系统网站
    LIN总线入门
    【面试题-Vue】常见问题二、组件类
    DAY12-深度学习100例-卷积神经网络(CNN)识别验证码
    【微服务】Docker的基本操作
    Spring Cloud Netflix Ribbon
    力扣:128. 最长连续序列(Python3)
  • 原文地址:https://blog.csdn.net/chai_tian/article/details/133818478