unreal engine 增强输入的使用分析

由于ue5以前的输入 系统已经提示被弃用了,因此建议使用新版本

首先
分别新建输入操作
新建映射情景

image.png

打开新建的输入操作
根据下图可以得出结论,此东西用于描述 是何种类型映射,以及是否消耗事件 不传递

image.png
image.png

打开情景上下文MappingConext
新建映射选中刚刚创建的,

image.png

然后进行展开发现这里同样可以定义触发器和修改器
这里改成已按下。

image.png

新建pawn设置自动控制 为玩家0
打开蓝图在事件开始运行时执行
用代码表达

var 增强系统=获取玩家控制器().GetInputLocalPlayerSubsystem();
增强系统.添加映射上下文(mappingConext=刚刚新建的上下文,priority=优先级)
image.png

蓝图如下

image.png

再右键新建选择明明的Lozn_jump

image.png

运行后按空格键屏幕成功打印文字

image.png

上面是操作映射,也就是bool类型, 新建输入操作可以指定三维向量

最后来看看 官方完整的

移动的实现

首先新建move 增强事件 指定为2维向量

在上下文中分别新建awsd ,和前后左右方向键
d:默认
a:修改器选择反向(否定)
w:修改器选择拌合输入轴
s:修改器选择拌合输入轴 和否定
如下图所示

image.png

pawn对于IA_Move的使用

image.png

伪代码

onMove(isTragged,isStarted,float actionValuex, float actionValueY){
if(!isTragged){return;}
//控制向右
this.获取向右向量().z=this.获取控制旋转().z;//yaw 偏航
this.获取向右向量().x=this.获取控制旋转().x;//roll 翻滚 //理论上这句话不应该写的,在c++源码中是没有定义的
var worldDirection=this.获取向右向量();
this.添加移动输入(wordDirection,scaleValue:x)

//控制向前
var 向前向量=this.获取向前向量();
向前向量.z=this.获取控制旋转().z;//yaw 偏航

this.添加移动输入(wordDirection:向前向量,scaleValue:y)
}

旋转的实现

同样新建Look增加输入
然后在上下文中 指定为鼠标 XY 2D轴

修改器指定为否定

image.png

蓝图的定义

image.png

伪代码

onLook(isTragged,isStarted,float actionValuex, float actionValueY){
if(!isTragged){return;}


this.添加控制器Yaw输入(val:actionValuex);//偏航角度 
//实际上应该围绕z,但是这里是鼠标 2维,所以是围绕y而不是z   鼠标x水平的移动控制左右的移动
this.添加控制器Pitch输入(val:actionValueY);
//围绕y旋转 俯仰角 但是这里是鼠标y,也就是鼠标前后移动的数值Y来控制人物的俯仰
}

跳跃

image.png

在触发是调用跳跃
在完成时候调用停止跳跃

c++的实现使用

ACppDigitalTwnCharacter : public ACharacter
在构造方法中

GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
        
    //控制器转动时不旋转。让这只影响到相机
    bUseControllerRotationPitch = false;
    bUseControllerRotationYaw = false;
    bUseControllerRotationRoll = false;

    // 配置角色移动
    GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...   
    GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...at this rotation rate


    GetCharacterMovement()->JumpZVelocity = 700.f;
    GetCharacterMovement()->AirControl = 0.35f;
    GetCharacterMovement()->MaxWalkSpeed = 500.f;
    GetCharacterMovement()->MinAnalogWalkSpeed = 20.f;
    GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;

    // 创建一个弹簧臂
    CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
    CameraBoom->SetupAttachment(RootComponent);
    CameraBoom->TargetArmLength = 400.0f; // The camera follows at this distance behind the character   
    CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller

    // 创建跟随相机
    FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
    FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
    FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm

在事件开始运行 也就是BegainPlay 中绑定事件上下文

void ACppDigitalTwnCharacter::BeginPlay()
{

    Super::BeginPlay();
    //添加输入映射上下文
    if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
    {
        if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
        {
            Subsystem->AddMappingContext(DefaultMappingContext, 0);
        }
    }
}

上面的成员变量如DefaultMappingContext由c++子类蓝图中设置

image.png

定义

/** MappingContext */
    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
    class UInputMappingContext* DefaultMappingContext;

    /** Jump Input Action */
    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
    class UInputAction* JumpAction;

    /** Move Input Action */
    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
    class UInputAction* MoveAction;

    /** Look Input Action */
    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
    class UInputAction* LookAction;

重写SetupPlayerInputComponent方法完成事件绑定

void ACppDigitalTwnCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
    // Set up action bindings
    if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent)) {
        
        //Jumping
        EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
        EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);

        //Moving
        EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ACppDigitalTwnCharacter::Move);

        //Looking
        EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ACppDigitalTwnCharacter::Look);

    }

}

旋转和移动的实现

void ACppDigitalTwnCharacter::Move(const FInputActionValue& Value)
{
    // 转为2d向量
    FVector2D MovementVector = Value.Get<FVector2D>();

    if (Controller != nullptr)
    {
        // 找出是哪个方向向前 获取控制旋转
        const FRotator Rotation = Controller->GetControlRotation();
        const FRotator YawRotation(0, Rotation.Yaw, 0);//inPich和inRoll都为0但是yaw 也就是方向取控制旋转的方向
//ue坐标系 前面 是x 垂直是z水平 是y
        // 获取向前向量
        const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);

    
        //获取向右向量
        const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);

        // add movement 
        AddMovementInput(ForwardDirection, MovementVector.Y);
        AddMovementInput(RightDirection, MovementVector.X);
    }
}

void ACppDigitalTwnCharacter::Look(const FInputActionValue& Value)
{
    // input is a Vector2D
    FVector2D LookAxisVector = Value.Get<FVector2D>();

    if (Controller != nullptr)
    {
        // add yaw and pitch input to controller
        AddControllerYawInput(LookAxisVector.X);//围绕x轴 [ue-x代表向前的水平线]偏航叫 控制左右旋转 z代表高度垂直线
        AddControllerPitchInput(LookAxisVector.Y);//围绕y轴 [ue是水平线]俯仰角 roll  在unreal中,pitch 对应于y轴,yaw对应于z轴,roll对应于x轴。
    }
}

猜你喜欢

转载自blog.csdn.net/u010042660/article/details/128482151