【发布时间】: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()中调用的。