RPG4.設置角色輸入

這一篇是進行玩家移動和視角移動的介紹。

1.在玩家內進行移動覆寫

virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;

2.創建增強輸入資產的變量創建

UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "CharacterData", meta = (AllowPrivateAccess = "true"))UDA_InputConfig* InputConfigDataAsset;

3.進行移動和視角移動函數的創建

void Input_Move(const FInputActionValue& InputActionValue);void Input_Look(const FInputActionValue& InputActionValue);

4.在SetupPlayerInputComponent函數內進行輸入綁定

//設置玩家輸入組件
void AXMBCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{//添加映射上下文checkf(InputConfigDataAsset, TEXT("Forgot to assign a valid data as input config"))//獲取本地玩家ULocalPlayer* LocalPlayer = GetController<APlayerController>()->GetLocalPlayer();//獲取增強輸入子系統UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(LocalPlayer);check(Subsystem);//添加映射上下文Subsystem->AddMappingContext(InputConfigDataAsset->DefaultMappingContext,0);//獲取角色輸入組件并綁定輸入動作UXMBWarriorInputComponent* WarriorInputComponent = CastChecked<UXMBWarriorInputComponent>(PlayerInputComponent);WarriorInputComponent->BindNativeInputAction(InputConfigDataAsset,XMBGameplayTags::InputTag_Move, ETriggerEvent::Triggered,this,&ThisClass::Input_Move);WarriorInputComponent->BindNativeInputAction(InputConfigDataAsset,XMBGameplayTags::InputTag_Look, ETriggerEvent::Triggered,this,&ThisClass::Input_Look);}

5.對Move函數進行設置

void AXMBCharacter::Input_Move(const FInputActionValue& InputActionValue)
{//獲取移動向量const FVector2D MovementVector = InputActionValue.Get<FVector2D>();//獲取移動旋轉const FRotator MovementRotation(0.f, Controller->GetControlRotation().Yaw, 0.f);//處理前后移動if (MovementVector.Y != 0.f){const FVector ForwardDirection = MovementRotation.RotateVector(FVector::ForwardVector);AddMovementInput(ForwardDirection, MovementVector.Y);}//處理左右移動if (MovementVector.X != 0.f){const FVector RightDirection = MovementRotation.RotateVector(FVector::RightVector);AddMovementInput(RightDirection, MovementVector.X);}}

6.對視角移動函數進行設置

void AXMBCharacter::Input_Look(const FInputActionValue& InputActionValue)
{const FVector2D LookAxisVector = InputActionValue.Get<FVector2D>();if (LookAxisVector.X != 0.f){AddControllerYawInput(LookAxisVector.X);}if (LookAxisVector.Y != 0.f){AddControllerPitchInput(LookAxisVector.Y);}
}

7.打開引擎,進入角色藍圖,細節面板搜索data,更換成自己的dataasset

#include "Character/XMBCharacter.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "EnhancedInputSubsystems.h"
#include "Data/Input/DA_InputConfig.h"
#include "Components/Input/XMBWarriorInputComponent.h"
#include "XMBGameplayTags.h"#include "XMBDebugHelper.h"AXMBCharacter::AXMBCharacter()
{GetCapsuleComponent()->InitCapsuleSize(42.f, 96.f);bUseControllerRotationPitch = false;bUseControllerRotationYaw = false;bUseControllerRotationRoll = false;CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));CameraBoom->SetupAttachment(GetRootComponent());CameraBoom->TargetArmLength = 300.0f;CameraBoom->SocketOffset = FVector(0.0f, 55.0f, 60.0f);CameraBoom->bUsePawnControlRotation = true;FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);FollowCamera->bUsePawnControlRotation = false;GetCharacterMovement()->bOrientRotationToMovement = true;GetCharacterMovement()->RotationRate = FRotator(0.0f, 550.0f, 0.0f);GetCharacterMovement()->MaxWalkSpeed = 400.f;GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;}void AXMBCharacter::BeginPlay()
{Super::BeginPlay();Debug::Print(FString::Printf(TEXT("XMBCharacter::BeginPlay")));
}//設置玩家輸入組件
void AXMBCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{//添加映射上下文checkf(InputConfigDataAsset, TEXT("Forgot to assign a valid data as input config"))//獲取本地玩家ULocalPlayer* LocalPlayer = GetController<APlayerController>()->GetLocalPlayer();//獲取增強輸入子系統UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(LocalPlayer);check(Subsystem);//添加映射上下文Subsystem->AddMappingContext(InputConfigDataAsset->DefaultMappingContext,0);//獲取角色輸入組件并綁定輸入動作UXMBWarriorInputComponent* WarriorInputComponent = CastChecked<UXMBWarriorInputComponent>(PlayerInputComponent);WarriorInputComponent->BindNativeInputAction(InputConfigDataAsset,XMBGameplayTags::InputTag_Move, ETriggerEvent::Triggered,this,&ThisClass::Input_Move);WarriorInputComponent->BindNativeInputAction(InputConfigDataAsset,XMBGameplayTags::InputTag_Look, ETriggerEvent::Triggered,this,&ThisClass::Input_Look);}void AXMBCharacter::Input_Move(const FInputActionValue& InputActionValue)
{//獲取移動向量const FVector2D MovementVector = InputActionValue.Get<FVector2D>();//獲取移動旋轉const FRotator MovementRotation(0.f, Controller->GetControlRotation().Yaw, 0.f);//處理前后移動if (MovementVector.Y != 0.f){const FVector ForwardDirection = MovementRotation.RotateVector(FVector::ForwardVector);AddMovementInput(ForwardDirection, MovementVector.Y);}//處理左右移動if (MovementVector.X != 0.f){const FVector RightDirection = MovementRotation.RotateVector(FVector::RightVector);AddMovementInput(RightDirection, MovementVector.X);}// if (MovementVector.Z != 0.f)// {// 	const FVector RightDirection = MovementRotation.RotateVector(FVector::UpVector);//// 	AddMovementInput(RightDirection, MovementVector.Z);// }}void AXMBCharacter::Input_Look(const FInputActionValue& InputActionValue)
{const FVector2D LookAxisVector = InputActionValue.Get<FVector2D>();if (LookAxisVector.X != 0.f){AddControllerYawInput(LookAxisVector.X);}if (LookAxisVector.Y != 0.f){AddControllerPitchInput(LookAxisVector.Y);}
}
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "InputActionValue.h"
#include "Character/CharacterBase.h"
#include "XMBCharacter.generated.h"class UDA_InputConfig;
class UCameraComponent;
class USpringArmComponent;
struct FInputActionValue;
/*** */
UCLASS()
class ARPG_GRIVITY_API AXMBCharacter : public ACharacterBase
{GENERATED_BODY()
public:AXMBCharacter();protected:virtual void BeginPlay() override;virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;private:UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))USpringArmComponent* CameraBoom;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Camera", meta = (AllowPrivateAccess = "true"))UCameraComponent* FollowCamera;//UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "CharacterData", meta = (AllowPrivateAccess = "true"))UDA_InputConfig* InputConfigDataAsset;void Input_Move(const FInputActionValue& InputActionValue);void Input_Look(const FInputActionValue& InputActionValue);};

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/903659.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/903659.shtml
英文地址,請注明出處:http://en.pswp.cn/news/903659.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[實戰] Petalinux驅動開發以及代碼框架解讀

目錄 Petalinux驅動開發以及代碼框架解讀一、引言二、步驟2.1 創建PetaLinux工程2.2 配置硬件描述文件2.3 設備樹配置2.4 建立驅動框架2.5 編輯 .bb 文件2.6 編寫驅動文件2.7 編寫 Makefile2.8 驗證配方配置2.9 集成驅動到 RootFS2.10 全系統編譯與部署2.11 啟動驗證 三、框架解…

[特殊字符] 開發工作高內存占用場景下,Windows 內存壓縮機制是否應該啟用?實測分析與優化建議

在日常開發中&#xff0c;我們往往需要同時運行多個高占用內存的工具&#xff0c;例如&#xff1a; IntelliJ IDEA VMware 虛擬機 多個 Java 后端程序 這些應用程序非常“吃內存”&#xff0c;輕松就能把 16GB、甚至 24GB 的物理內存用滿。那么&#xff0c;Windows 的“內存…

嵌入式學習筆記 - HAL_xxx_MspInit(xxx);函數

使用cubeMX生成的HAL庫函數中&#xff0c;所有外設的初始化函數HAL_xxx_Init(&xxxHandle)中都存在有此調用函數HAL_xxx_MspInit(xxx)&#xff0c;此調用函數其實是對各外設模塊比如UART&#xff0c;I2C等控制器使用的的底層硬件進行初始化&#xff0c;包括時鐘&#xff0c;…

Nginx — http、server、location模塊下配置相同策略優先級問題

一、配置優先級簡述 在 Nginx 中&#xff0c;http、server、location 模塊下配置相同策略時是存在優先級的&#xff0c;一般遵循 “范圍越小&#xff0c;優先級越高” 的原則&#xff0c;下面為你詳細介紹&#xff1a; 1. 配置繼承關系 http 塊&#xff1a;作為全局配置塊&…

WPF之TextBlock控件詳解

文章目錄 1. TextBlock控件介紹2. TextBlock的基本用法2.1 基本語法2.2 在代碼中創建TextBlock 3. TextBlock的常用屬性3.1 文本內容相關屬性3.2 字體相關屬性3.3 外觀相關屬性3.4 布局相關屬性 4. TextBlock文本格式化4.1 使用Run元素進行內聯格式化4.2 其他內聯元素 5. 處理長…

華為云loT物聯網介紹與使用

&#x1f310; 華為云 IoT 物聯網平臺詳解&#xff1a;構建萬物互聯的智能底座 隨著萬物互聯時代的到來&#xff0c;物聯網&#xff08;IoT&#xff09;已成為推動數字化轉型的關鍵技術之一。華為云 IoT 平臺&#xff08;IoT Device Access&#xff09;作為華為云的核心服務之…

AnimateCC教學:形狀補間動畫的代碼實現

核心代碼: var shape; var animationProps = {width: 50,height: 50,cornerRadius: 0,color: "#00FF00" }; function init() { shape = new createjs.Shape();shape.x = 200;shape.y = 150;stage.addChild(shape);// 初始繪制updateShape();// 設置補間動畫createTw…

Android學習總結之Retrofit篇

1. 注解原理概述 在 Java 里&#xff0c;注解是一種元數據&#xff0c;它為代碼提供額外信息但不影響程序的實際邏輯。注解可以在類、方法、字段等元素上使用&#xff0c;并且能在編譯時、運行時通過反射機制被讀取。Retrofit 充分利用了 Java 注解機制&#xff0c;通過自定義…

windows11 編譯 protobuf-3.21.12 c++

下載 protobuf 包&#xff0c;本文使用 3.21.12 版本&#xff0c;Gitub下載鏈接&#xff1a; Github官網 , 網盤下載&#xff1a; 網盤 如果電腦環境沒有安裝 cmake 則需要安裝&#xff0c;本文測試使用 cmake-3.25.1 版本&#xff0c; 下載地址&#xff1a;[camke-3.25.1] (…

Java繼承中super的使用方法

super 關鍵字在 Java 中用于訪問父類的成員&#xff08;包括字段、方法和構造函數&#xff09;。當你在子類中調用父類的方法或訪問父類的成員變量時&#xff0c;super 是必不可少的工具。 &#x1f511; super 的基本用法 1. 調用父類的構造方法 在子類的構造方法中&#x…

網絡安全之淺析Java反序列化題目

前言 這段時間做了幾道Java反序列化題目&#xff0c;發現很多題目都是類似的&#xff0c;并且可以通過一些非預期gadget打進去&#xff0c;就打算總結一下常見的題目類型以及各種解法&#xff0c;并提煉出一般性的思維方法。 正文 分析入口點 拿到題目&#xff0c;有附件最…

動態規劃問題,下降路徑最小和(dp初始化問題,狀態壓縮),單詞拆分(回溯法+剪枝+記憶化),substr函數

下降路徑最小和 題目鏈接&#xff1a; 931. 下降路徑最小和 - 力扣&#xff08;LeetCode&#xff09; 題目描述&#xff1a; 給你一個 n x n 的 方形 整數數組 matrix &#xff0c;請你找出并返回通過 matrix 的下降路徑 的 最小和 。 下降路徑 可以從第一行中的任何元素開…

大數據治理自動化與智能化實踐指南:架構、工具與實戰方案(含代碼)

??個人主頁??:一ge科研小菜雞-CSDN博客 ????期待您的關注 ???? 一、引言:從人治到機治,數據治理正在進化 隨著數據體量持續膨脹、數據場景復雜化,傳統依賴人工規則的大數據治理方式已難以為繼。企業在治理過程中面臨: 數據質量問題激增,人工檢測成本高 元數…

Golang - 實現文件管理服務器

先看效果&#xff1a; 代碼如下&#xff1a; package mainimport ("fmt""html/template""log""net/http""os""path/filepath""strings" )// 配置根目錄&#xff08;根據需求修改&#xff09; //var ba…

Linux-04-用戶管理命令

一、useradd添加新用戶: 基本語法: useradd 用戶名:添加新用戶 useradd -g 組名 用戶:添加新用戶到某個組二、passwd設置用戶密碼: 基本語法: passwd 用戶名:設置用戶名密碼 三、id查看用戶是否存在: 基本語法: id 用戶名 四、su切換用戶: 基本語法: su 用戶名稱:切換用…

Ollama 安裝 QWen3 及配置外網訪問指南

一、Ollama 安裝 QWen3 安裝步驟 首先嘗試運行 QWen3 模型&#xff1a; ollama run qwen3 如果遇到版本不兼容錯誤&#xff08;Error 412&#xff09;&#xff0c;表示需要升級 Ollama&#xff1a; curl -fsSL https://ollama.com/install.sh | sh 驗證版本&#xff1a; o…

高性能架構設計-數據庫(讀寫分離)

一、高性能數據庫簡介 1.高性能數據庫方式 讀寫分離&#xff1a;將訪問壓力分散到集群中的多個節點&#xff0c;沒有分散存儲壓力 分庫分表&#xff1a;既可以分散訪問壓力&#xff0c;又可以分散存儲壓力 2.為啥不用表分區 如果SQL不走分區鍵&#xff0c;很容易出現全表鎖…

【Hive入門】Hive性能優化:執行計劃分析EXPLAIN命令的使用

目錄 1 EXPLAIN命令簡介 1.1 什么是EXPLAIN命令&#xff1f; 1.2 EXPLAIN命令的語法 2 解讀執行計劃中的MapReduce階段 2.1 執行計劃的結構 2.2 Hive查詢執行流程 2.3 MapReduce階段的詳細解讀 3 識別性能瓶頸 3.1 數據傾斜 3.2 Shuffle開銷 3.3 性能瓶頸識別與優化 4 總結 在大…

開源模型應用落地-qwen模型小試-Qwen3-8B-快速體驗(一)

一、前言 阿里云最新推出的 Qwen3-8B 大語言模型,作為國內首個集成“快思考”與“慢思考”能力的混合推理模型,憑借其 80 億參數規模及 128K 超長上下文支持,正在重塑 AI 應用邊界。該模型既可通過輕量化“快思考”實現低算力秒級響應,也能在復雜任務中激活深度推理模式,以…

Kafka Producer的acks參數對消息可靠性有何影響?

1. acks0 可靠性最低生產者發送消息后不等待任何Broker確認可能丟失消息&#xff08;Broker處理失敗/網絡丟失時無法感知&#xff09;吞吐量最高&#xff0c;適用于允許數據丟失的場景&#xff08;如日志收集&#xff09; 2. acks1 (默認值) Leader副本確認模式生產者等待Le…