UE中绑定输入键的部分方式

本文主要介绍了在UE C++项目中进行输入绑定的方法。先说明了创建UE C++项目及继承ACharacter的角色类后,在函数中绑定输入的默认方式,还需在UE编辑器里绑定映射。接着介绍了FInputKeyBinding、UInputComponent::BindKey等其他操作映射和轴映射的绑定方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们先正常的创建UE C++项目,然后在UE编辑器中创建继承ACharacter的角色类(ACharacter_Y);

在void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

函数中绑定输入

这里是官方文档教的默认绑定方式

轴映射

PlayerInputComponent->BindAxis("MoveForward", this, &ACharacter_Y::MoveForward);

PlayerInputComponent->BindAxis(映射名,一般填当前对象,按键后调用的函数)

操作映射

PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter_Y::StartJump);

PlayerInputComponent->BindAction(映射名,(输入事件)按钮按下/抬起,一般填当前对象,

输入事件发射后调用的函数)

在C++代码中写上这些后还需要在UE编辑器里  编辑》项目设置 》输入》绑定映射

以上都是默认的   你想想看每次修改添加都需要去编辑器里添加  头都大

所以下面有我目前知道的其它方法

一:FInputKeyBinding    UInputComponent::KeyBindings   FInputChord

要先填FInputKeyBinding 类

如上图      EKeys::R  这个就是按键R     ,IE_Pressed这个是按键按下

.KeyDelegate.BindDelegate(this, &ACharacter_Y::Teleportation);绑定函数

.KeyDelegate.BindDelegate(一般填当前对象,输入事件发射后调用的函数);

填写完FInputKeyBinding 类只是前置操作

还要需要添加进UInputComponent::KeyBindings

UInputComponent::KeyBindings.Add(FInputKeyBinding 对象)

现在就可以按R键使用函数了

二:UInputComponent::BindKey

这个和上诉的差不多

三:FInputActionKeyMapping 

UGameplayStatics::GetPlayerController(this, 0)/*获取玩家控制器*/->PlayerInput->AddActionMapping

UInputComponent::BindAction  这个是比较重要的

这个是和手动编辑器里的映射按键方法一样  不过变成代码写入了 

注意构造函数  因为一定会想Shift  Ctrl Alt Cmd 键呢  其实就在构造函数中 填true就可以了

FInputActionKeyMapping(const FName InActionName = NAME_None, const FKey InKey = EKeys::Invalid, const bool bInShift = false, const bool bInCtrl = false, const bool bInAlt = false, const bool bInCmd = false)

填写完也要添加

UGameplayStatics::GetPlayerController(this, 0)/*获取玩家控制器*/->PlayerInput->AddActionMapping(ActionKeyMap);

备注:以上都是操作映射的操作

四:轴映射   FInputAxisKeyMapping 

UGameplayStatics::GetPlayerController(this, 0)/*获取玩家控制器*/->PlayerInput->AddAxisMapping

UInputComponent::BindAxis

是的这个C++就是轴映射  它三方法其实类似 都是绑定到一个名称 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值