首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >统一武器开关

统一武器开关
EN

Stack Overflow用户
提问于 2020-11-30 04:20:19
回答 1查看 191关注 0票数 0

我有武器开关代码,但是当开关到达一个空的插槽时,开关停止工作。开关按钮为TAB。如果我将武器从插槽2切换到插槽3,从插槽3切换到插槽1,它工作得很好,但是当从插槽1(它是空的)切换到下一个插槽(武器在哪里)时,它就不起作用了。

代码如下:

代码语言:javascript
运行
复制
// 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);
}
EN

回答 1

Stack Overflow用户

发布于 2020-11-30 14:59:22

如果你有武器而下一个武器不存在,那么你就错过了控件,所以代码是崩溃的。你应该这样做:

代码语言:javascript
运行
复制
if(newWeapon == 0)
  {
    //set so it is going to a new condition but without having a gun showing
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65064760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档