我有武器开关代码,但是当开关到达一个空的插槽时,开关停止工作。开关按钮为TAB。如果我将武器从插槽2切换到插槽3,从插槽3切换到插槽1,它工作得很好,但是当从插槽1(它是空的)切换到下一个插槽(武器在哪里)时,它就不起作用了。
代码如下:
// Change the active weapon.
public void ChangeWeapon(int oldWeapon, int newWeapon)
{
// Previously armed? Disable weapon.
if (oldWeapon > 0)
{
weapons[oldWeapon].gameObject.SetActive(false);
gunMuzzle = null;
weapons[oldWeapon].Toggle(false);
}
// Cycle trought empty slots to find next existing weapon or the no weapon slot.
while (weapons[newWeapon] == null && newWeapon > 0)
{
newWeapon = (newWeapon + 1) % weapons.Count;
}
// Next weapon exists? Activate it.
if (newWeapon > 0)
{
weapons[newWeapon].gameObject.SetActive(true);
gunMuzzle = weapons[newWeapon].transform.Find("muzzle");
weapons[newWeapon].Toggle(true);
}
activeWeapon = newWeapon;
// Call change weapon animation if new weapon type is different.
if (oldWeapon != newWeapon)
{
behaviourManager.GetAnim.SetTrigger(changeWeaponTrigger);
behaviourManager.GetAnim.SetInteger(weaponTypeInt, weapons[newWeapon] (int)weapons[newWeapon].type : 0);
}
// Set crosshair if armed.
SetWeaponCrosshair(newWeapon > 0);
}
发布于 2020-11-30 14:59:22
如果你有武器而下一个武器不存在,那么你就错过了控件,所以代码是崩溃的。你应该这样做:
if(newWeapon == 0)
{
//set so it is going to a new condition but without having a gun showing
}
https://stackoverflow.com/questions/65064760
复制相似问题