• UE5C++学习(一)--- 增强输入系统


    一、关于增强输入系统的介绍

    增强输入系统官方文档介绍

    二、增强输入系统的具体使用

    注:在使用方面,不会介绍如何创建项目等基础操作,如果还没有UE的使用基础,可以参考一下我之前UE4的文章,操作差别不会很大。

    如上图所示,在自己创建好的项目工程中,找到.Build.cs文件,在添加的模块引用中,添加EnhancedInput模块,添加这个模块之后,才能在写完增强输入系统的代码后正确运行。

    代码:

    1. //输入映射
    2. UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input", meta = (AllowPrivateAccess = "true"))
    3. class UInputMappingContext* DefaultMappingContext;
    4. //移动
    5. UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input", meta = (AllowPrivateAccess = "true"))
    6. class UInputAction* MoveAction;
    7. //上下左右看
    8. UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input", meta = (AllowPrivateAccess = "true"))
    9. class UInputAction* LookAction;

    在我们创建完成的角色类中添加必要的组件,比如摄像机臂组件和摄像机组件。UInputMappingContext是用来引用操作上下文,而UInputAction对应某个具体的操作,比如我们的WASD前后左右移动,鼠标轴挥动去上下左右看,当我们的Action创建完成之后,去放到操作上下文中去映射,这个时候我们的输入便被绑定到角色中。

    代码:

    1. UEnhancedInputComponent* EnhancedInputComponent = CastChecked(PlayerInputComponent);
    2. if (EnhancedInputComponent && MoveAction && LookAction)
    3. {
    4. EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered,this,&ASCharacter::Move);
    5. EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ASCharacter::Look);
    6. }

    在角色输入绑定函数中,用增强输入组件去绑定Action,之后输入操作按键便会执行对应的操作。

    对于ETriggerEvent,在引擎源代码中有相应的介绍,有按键按下,一直按住,松开时的处理,会比UE4的输入更加详细。

    在Move和Look的函数中,处理角色移动和上下左右看。

    Move代码:

    1. FVector2D MovementVector = Value.Get();
    2. if (Controller)
    3. {
    4. const FRotator ControlRotation = Controller->GetControlRotation();
    5. const FRotator YawRotation = FRotator(0.0f,ControlRotation.Yaw,0.0f);
    6. const FVector ForawrdDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
    7. const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
    8. AddMovementInput(ForawrdDirection,MovementVector.Y);
    9. AddMovementInput(RightDirection, MovementVector.X);
    10. }

    Look代码:

    1. FVector2D LookVector = Value.Get();
    2. if (Controller)
    3. {
    4. AddControllerYawInput(LookVector.X);
    5. AddControllerPitchInput(LookVector.Y);
    6. }

    以上处理完成之后,需要在游戏运行的时候,添加增强输入系统的映射。

    1. APlayerController* PlayerController = Cast(Controller);
    2. UEnhancedInputLocalPlayerSubsystem* EnhancedInputSystem =
    3. ULocalPlayer::GetSubsystem(PlayerController->GetLocalPlayer());
    4. if (EnhancedInputSystem && DefaultMappingContext)
    5. {
    6. EnhancedInputSystem->AddMappingContext(DefaultMappingContext,0);
    7. }

    这个时候,回到引擎中,去创建一个输入映射和move、look的Action。

    在移动和上下左右看的Action中,添加需要操作的按键。

    MappingContext中绑定,注意方向输入:

    注意在角色蓝图中去选择创建的输入和映射。

  • 相关阅读:
    Python----异常处理、断言和路径处理,简单、清晰版
    安装 DolphinDB Python API
    2022年认证杯SPSSPRO杯数学建模C题(第一阶段)污水流行病学原理在新冠疫情防控方面的作用求解全过程文档及程序
    JavaScript 69 JavaScript Web API 69.2 JavaScript 验证 API
    通过制作4个游戏学习Python
    五十七、视图组件
    Live555(一)
    Mybatis复习面试题
    C# FileSystemWatcher 实时监控文件的增加、修改、重命名和删除实例
    Javaweb安全——Tomcat 内存马基础
  • 原文地址:https://blog.csdn.net/qq_42597694/article/details/134220145