Sab*_*shī 6 c# unity-game-engine
试图OnAttack()在按住按钮时连续重复功能功能。
基本上我正在寻找一个等效于 Update() { GetKeyDown() {//code }}But 的输入系统。
编辑:使用操纵杆,无法判断正在按下哪个按钮。
Sab*_*shī 10
好的,我通过在交互中使用“按下”并给出触发行为“按下并释放”来解决它,然后做了
bool held = false;
Update()
{
if(held)
{
//animation
}
else if(!held)
{
//idle animation
}
}
OnAttack() {
held = !held;
}
Run Code Online (Sandbox Code Playgroud)
这样,如果我按下保持的按钮变为真,它会每帧重复动画,放手会使“保持”不真实并执行空闲动画
小智 6
本质上,您分配给按钮的功能将在每次按下按钮时触发两次,一次是按下(执行)时,一次是释放(取消)时。您可以在函数开头传递此上下文,只需确保您使用的是在此脚本顶部看到的库即可。现在,您可以打开和关闭布尔值,指示是否按下按钮,然后在更新期间根据布尔值的状态执行操作
using static UnityEngine.InputSystem.InputAction;
bool held = false;
Update()
{
if(held)
{
//Do hold action like shooting or whatever
}
else if(!held)
{
//do alternatice action. Not Else if required if no alternative action
}
}
//switch the status of held based on whether the button is being pressed or released. OnAttack is called every time the button is pressed and every time it is released, the if statements are what determine which of those two is currently happening.
OnAttack(CallbackContext ctx) {
if (ctx.performed)
held= true;
if (ctx.canceled)
held= false;
}
Run Code Online (Sandbox Code Playgroud)