深圳網(wǎng)站設(shè)計公司電商培訓機構(gòu)靠譜嗎
一、關(guān)于增強輸入系統(tǒng)的介紹
增強輸入系統(tǒng)官方文檔介紹
二、增強輸入系統(tǒng)的具體使用
注:在使用方面,不會介紹如何創(chuàng)建項目等基礎(chǔ)操作,如果還沒有UE的使用基礎(chǔ),可以參考一下我之前UE4的文章,操作差別不會很大。
如上圖所示,在自己創(chuàng)建好的項目工程中,找到.Build.cs文件,在添加的模塊引用中,添加EnhancedInput模塊,添加這個模塊之后,才能在寫完增強輸入系統(tǒng)的代碼后正確運行。
代碼:
//輸入映射
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input", meta = (AllowPrivateAccess = "true"))class UInputMappingContext* DefaultMappingContext;
//移動
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input", meta = (AllowPrivateAccess = "true"))class UInputAction* MoveAction;
//上下左右看
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Input", meta = (AllowPrivateAccess = "true"))class UInputAction* LookAction;
在我們創(chuàng)建完成的角色類中添加必要的組件,比如攝像機臂組件和攝像機組件。UInputMappingContext是用來引用操作上下文,而UInputAction對應某個具體的操作,比如我們的WASD前后左右移動,鼠標軸揮動去上下左右看,當我們的Action創(chuàng)建完成之后,去放到操作上下文中去映射,這個時候我們的輸入便被綁定到角色中。
代碼:
UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent);
if (EnhancedInputComponent && MoveAction && LookAction)
{EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered,this,&ASCharacter::Move);EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ASCharacter::Look);}
在角色輸入綁定函數(shù)中,用增強輸入組件去綁定Action,之后輸入操作按鍵便會執(zhí)行對應的操作。
對于ETriggerEvent,在引擎源代碼中有相應的介紹,有按鍵按下,一直按住,松開時的處理,會比UE4的輸入更加詳細。
在Move和Look的函數(shù)中,處理角色移動和上下左右看。
Move代碼:
FVector2D MovementVector = Value.Get<FVector2D>();if (Controller){const FRotator ControlRotation = Controller->GetControlRotation();const FRotator YawRotation = FRotator(0.0f,ControlRotation.Yaw,0.0f);const FVector ForawrdDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);AddMovementInput(ForawrdDirection,MovementVector.Y);AddMovementInput(RightDirection, MovementVector.X);}
Look代碼:
FVector2D LookVector = Value.Get<FVector2D>();if (Controller){AddControllerYawInput(LookVector.X);AddControllerPitchInput(LookVector.Y);}
以上處理完成之后,需要在游戲運行的時候,添加增強輸入系統(tǒng)的映射。
APlayerController* PlayerController = Cast<APlayerController>(Controller);UEnhancedInputLocalPlayerSubsystem* EnhancedInputSystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer());if (EnhancedInputSystem && DefaultMappingContext){EnhancedInputSystem->AddMappingContext(DefaultMappingContext,0);}
這個時候,回到引擎中,去創(chuàng)建一個輸入映射和move、look的Action。
在移動和上下左右看的Action中,添加需要操作的按鍵。
MappingContext中綁定,注意方向輸入:
注意在角色藍圖中去選擇創(chuàng)建的輸入和映射。