【问题标题】:Input not being detected (yet input is specified)未检测到输入(尚未指定输入)
【发布时间】:2015-05-15 06:02:17
【问题描述】:

我有以下功能来管理我的游戏中玩家枪支的重新加载过程。我从 AmmoCheck 函数调用该函数并运行它,但是 if 语句永远不会运行,因为没有检测到输入。行:

Debug.Log("重载功能检查");

将打印到控制台,但仅此而已。

我已确保在编辑>项目设置>输入下设置了输入,并将其设置为 r 按钮。 (证明 - http://i.imgur.com/u2aVNpU.png

功能:

void gunReload()
{
    Debug.Log("Reload function check");
    if (Input.GetButtonDown("Reload")) {

        Debug.Log("Reloading");
        if(changeWeapon.currentGun == changeWeapon.gunPistol) {
            aReload.SetActive(false);
            // Incrementing the aClipPistolCurrent value by 1 so the current clip "should" progress one along? idk
            aClipPistolCurrent += 1;
        }
        if(changeWeapon.currentGun == changeWeapon.gunAssault) {
            aReload.SetActive(false);
            // Incrementing the aClipPistolCurrent value by 1 so the current clip "should" progress one along? idk
            aClipAssaultCurrent += 1;
        }

    }       
}

函数调用 gunReload();

gunAmmoCheck() 在 Update() 内部被调用

void gunAmmoCheck()
{

    if(changeWeapon.currentGun == changeWeapon.gunPistol && aClipPistol[aClipPistolCurrent] > 0) {
        gunFire ();
        // Reducing the ammo of the current clip by 1.
        // ClipPistol is being used (say array, why array
        aClipPistol[aClipPistolCurrent] -= 1;
    }
    if(changeWeapon.currentGun == changeWeapon.gunAssault && aClipAssault[aClipAssaultCurrent] > 0) {
        gunFire ();
        // Reducing the ammo of the current clip by 1.
        // ClipPistol is being used (say array, why array
        aClipAssault[aClipAssaultCurrent] -= 1;
    }

    if(aClipPistol[aClipPistolCurrent] == 0)
    {
        Debug.Log ("Reload");
        // Activating the reload notification on the interface
        aReload.SetActive(true);
        gunReload();
    }
    if(aClipAssault[aClipAssaultCurrent] == 0)
    {
        Debug.Log ("Reload");
        // Activating the reload notification on the interface
        aReload.SetActive(true);
        noAmmo = true;
        gunReload();
    }
}

我在这里不知所措,因为我所有的其他输入都完美无缺。任何帮助将不胜感激。

【问题讨论】:

  • 出于好奇,GetKeyDown 有效吗? docs.unity3d.com/ScriptReference/Input.GetKeyDown.html
  • GetKeyDown 有效,但它仅适用于单个帧,帧完成后状态会重置,因此必须在 Update 方法中调用它。检查更新例程中的密钥状态并设置一个在重新加载例程中检查的标志。
  • 对不起,Ron,你能详细解释一下吗?
  • 他没有说这不是来自Update() 如果我们知道这是来自协程,我会同意,但我很确定这只是几个调用堆栈来自Update()
  • 对不起,调用reload函数的ammoCheck函数是在Update()中调用的。

标签: c# input unity3d


【解决方案1】:

我刚刚对此进行了测试,它确实有效。我开始认为问题在于您从哪里调用该函数。还不确定,但是

让我们慢慢发现问题。你能在你的函数中注释掉那些东西并且只有

 void gunReload ()
    {
        //Debug.Log ("Reload function check");
        if (Input.GetButtonDown ("Reload")) {

            Debug.Log ("Reloading");

        }       
    } 

在您的代码中。然后从更新中调用 gunReload () 并告诉我它是否有效。 提出您从中调用 gunReload () 的函数也很好。

【讨论】:

  • 执行您所说的会记录“重新加载”消息,因此输入适用于此。用我最初调用它的函数更新原始问题。
  • 还有一件事。您能否在调用 gunAmmoCheck() 的 Update() 中发布代码行。例如,您是用 gunAmmoCheck() 调用它还是在调用它之前必须检查一些逻辑。例如, if(...){gunAmmoCheck()} ?
  • 我目前有它: if (Input.GetButtonDown ("Fire1")) { gunAmmoCheck();我现在意识到这可能意味着我必须在我的重新加载按钮的同时关闭 Fire1?
  • 大声笑是的。那就是问题所在。您真的希望玩家在重新加载之前开火吗?我以前从未见过这样的游戏控制。删除枪AmmoCheck();从那个 if 语句中检查它现在是否正常工作
  • /叹息。我是个白痴,我发誓。我必须至少翻阅剧本二十次试图弄清楚。您会建议我在 Fire1 检查和重新加载检查中做什么?
猜你喜欢
  • 1970-01-01
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 2018-07-01
  • 1970-01-01
相关资源
最近更新 更多