莆田有哪幾家做網(wǎng)站設(shè)計(jì)的全球網(wǎng)站排名前100
UE4直接在項(xiàng)目設(shè)置里設(shè)置的軸映射和操作映射在UE5中被標(biāo)記為廢棄,改為使用增強(qiáng)輸入系統(tǒng)。
這兩天學(xué)習(xí)了下藍(lán)圖和c++中增強(qiáng)輸入系統(tǒng)的使用,在這里分享一下。
學(xué)習(xí)使用的模板是第三人稱模板(藍(lán)圖/c++),代碼藍(lán)圖都參考的模板。
增強(qiáng)輸入系統(tǒng)
UE5中加入的用來(lái)處理輸入的系統(tǒng)。
分為兩部分,分別是輸入操作和輸入映射情境。在虛幻編輯器中通過(guò)右鍵添加->輸入->輸入操作/輸入映射情境來(lái)創(chuàng)建。
輸入操作 InputAction
顧名思義,用來(lái)綁定特定的操作,例如鍵盤、鼠標(biāo)、手柄的某個(gè)按鍵。
在ThirdPerson/Blueprints/Input/Actions
目錄下,可以看到模板自帶的幾個(gè)輸入操作
我們雙擊點(diǎn)開(kāi)IA_Move
查看細(xì)節(jié)
看到值類型為Axis2D
,這個(gè)值類型是用來(lái)設(shè)置輸入的值的,因?yàn)橐苿?dòng)是一個(gè)X,Y軸上的二維操作,所以我們選擇Axis2D
,如果是跳躍的話,則使用數(shù)字(布爾)
。
模板通過(guò)創(chuàng)建輸入操作
成功綁定了幾種輸入操作,分別是觀察、跳躍和移動(dòng),接下來(lái)我們將在輸入映射情境中為輸入操作綁定按鍵和設(shè)置。
輸入映射情境 InputMappingContext
用來(lái)給輸入操作綁定具體的按鍵和一些處理的資產(chǎn)。
在ThirdPerson/Blueprints/Input
中,已經(jīng)有一個(gè)IMC_Default
的輸入映射情境,可以打開(kāi)看一下怎么使用。
通過(guò)映射旁邊的加號(hào)可以添加操作映射,新建后首先選擇我們的一個(gè)輸入操作
,例如第一個(gè)的IA_Jump
,之后點(diǎn)擊旁邊的加號(hào)為IA_Jump
操作進(jìn)行控制綁定??梢钥吹竭@里為IA_Jump
跳躍綁定了鍵盤空格、手柄正面按鈕下和觸摸1。IA_Move
移動(dòng)綁定了鍵盤WASD,上下左右鍵和手柄左搖桿2D軸。
點(diǎn)開(kāi)每個(gè)按鍵的下三角,可以看到可以配置觸發(fā)器和修改器,用于在按下后進(jìn)行一個(gè)設(shè)置。由于跳躍比較簡(jiǎn)單,所以沒(méi)有觸發(fā)器和修改器。我們看看WS按鍵的相關(guān)設(shè)置
W鍵在修改器中添加了一個(gè)拌合輸入軸值,這個(gè)是用例交換X、Y軸的,如果不設(shè)置這個(gè),默認(rèn)是X軸的移動(dòng),也就是向右走。
S鍵在W鍵的基礎(chǔ)上設(shè)置了一個(gè)否定,也就是去負(fù)值,表示向后走。
其他按鍵可自行查看。
使用
現(xiàn)在我們有了輸入操作和輸入映射情境后就已經(jīng)做好了一套輸入映射,例如WASD移動(dòng)、空格跳躍等,接下來(lái)我們要在藍(lán)圖和C++中使用我們做好的綁定。
藍(lán)圖中
我們查看/content/ThirdPerson/Blueprints/BP_ThirdPersonCharacter
的事件圖表,在最上面首先綁定了輸入映射情境
之后對(duì)每個(gè)輸入映射
都進(jìn)行了相應(yīng)的處理,我們先看移動(dòng)輸入
這里IA_Move
的ActionValue
是一個(gè)二維向量,因?yàn)樵?code>IA_Move里設(shè)置的值類型為Axis2D
,X軸表示左右移動(dòng),Y軸表示前后移動(dòng),這里具體的值取決于我們是鍵盤還是手柄,鍵盤是話應(yīng)該就是1,手柄則是根據(jù)左搖桿劃的程度來(lái)獲得一個(gè)0~1的值。選擇Triggered
來(lái)連線表示持續(xù)觸發(fā)。如果是跳躍操作,應(yīng)該選擇Started
,可自行查看。
如此我們就在藍(lán)圖中完成了使用增強(qiáng)輸入系統(tǒng)。
c++
c++中我們的角色基類為項(xiàng)目名+Character.h
,以下為源代碼
// Copyright Epic Games, Inc. All Rights Reserved.#pragma once#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Logging/LogMacros.h"
#include "ThirdPersonCppDemoCharacter.generated.h"class USpringArmComponent;
class UCameraComponent;
class UInputMappingContext;
class UInputAction;
struct FInputActionValue;DECLARE_LOG_CATEGORY_EXTERN(LogTemplateCharacter, Log, All);UCLASS(config=Game)
class AThirdPersonCppDemoCharacter : public ACharacter
{GENERATED_BODY()/** Camera boom positioning the camera behind the character */UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))USpringArmComponent* CameraBoom;/** Follow camera */UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))UCameraComponent* FollowCamera;/** MappingContext */UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))UInputMappingContext* DefaultMappingContext;/** Jump Input Action */UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))UInputAction* JumpAction;/** Move Input Action */UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))UInputAction* MoveAction;/** Look Input Action */UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))UInputAction* LookAction;public:AThirdPersonCppDemoCharacter();protected:/** Called for movement input */void Move(const FInputActionValue& Value);/** Called for looking input */void Look(const FInputActionValue& Value);protected:// APawn interfacevirtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;// To add mapping contextvirtual void BeginPlay();public:/** Returns CameraBoom subobject **/FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }/** Returns FollowCamera subobject **/FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }};
可以看到,這里創(chuàng)建了幾個(gè)UInputAction
類對(duì)象,對(duì)應(yīng)的就是輸入操作
。UInputMappingContext* DefaultMappingContext;
對(duì)應(yīng)的是輸入映射情境
,我們?cè)O(shè)置為藍(lán)圖可編輯,之后派生出藍(lán)圖類后在類默認(rèn)值那里可以設(shè)置。在BeginPlay
函數(shù)中,把DefaultMappingContext
給了增強(qiáng)輸入系統(tǒng)。
if (APlayerController* PlayerController = Cast<APlayerController>(Controller)){if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer())){Subsystem->AddMappingContext(DefaultMappingContext, 0);}}
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
為APawn
的接口,我們需要實(shí)現(xiàn)它,它就是用來(lái)設(shè)置按鍵按下后的反應(yīng)。此為模板為跳躍、移動(dòng)和觀察實(shí)現(xiàn)的該函數(shù)。
void AThirdPersonCppDemoCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{// Set up action bindingsif (UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent)) {// JumpingEnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Started, this, &ACharacter::Jump);EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);// MovingEnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AThirdPersonCppDemoCharacter::Move);// LookingEnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &AThirdPersonCppDemoCharacter::Look);}else{UE_LOG(LogTemplateCharacter, Error, TEXT("'%s' Failed to find an Enhanced Input component! This template is built to use the Enhanced Input system. If you intend to use the legacy system, then you will need to update this C++ file."), *GetNameSafe(this));}
}
上面代碼段中的Move、Look函數(shù)為模板實(shí)現(xiàn)的函數(shù)