首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >统一-武器切换由MouseWheel锁定位置(竞技场射击风格)

统一-武器切换由MouseWheel锁定位置(竞技场射击风格)
EN

Stack Overflow用户
提问于 2020-08-26 03:50:23
回答 1查看 631关注 0票数 0

我一直在努力找出如何实现一个竞技场式射击武器切换使用鼠标轮。

到目前为止,我已经创建了3个列表;一个列表是所有可用的枪支(枪支0级=手枪,1=中继器,2=狙击手,3=火箭发射器),下一个列表是一个具有固定位置的布尔列表,用来告诉您玩家是否已经解锁了该枪(同样,0,1,2,3是固定的)。我有第三个清单,从你所拥有的枪支开始,当你触摸拾起来的时候,又增加了一个新的。

我已经很容易得到阿尔法关键武器切换,和一个快速武器开关(切换之间的前一个武器和当前),但鼠标卷轴是我的疯狂。

我肯定我想得太多了,但我只是不知道在以下情况下该怎么做:玩家只有狙击手和手枪,这意味着我不能只做一个简单的-1,或者移动到中继器的索引,而玩家在向下滚动时没有这个索引。

我可以让它在与下一个武器交换的地方工作(即从0到3),但如果我接着拿起中继器或狙击手,它只会通过手枪和火箭发射器切换。

我真的不知道我在这里做什么我猜..。我不断尝试用不同的循环来迭代列表。我真正想知道的是,是否有一种简单的方法来遍历列表,得到下一个索引,或者如果我们到达列表的末尾,转到第一个索引。

所有的帮助都是非常感谢的。

谢谢,

雅各布

编辑:(根据要求,这里有相关的代码片段)

下面是我的游戏者控制器中的变量:

代码语言:javascript
运行
复制
//GUNS GUNS GUNS

public Gun activeGun;
public List<Gun> allGuns = new List<Gun>();
public List<bool> unlockedGuns = new List<bool>();
public List<Gun> gunsAvailable = new List<Gun>();
public int currentGun = 0;
private int previousGun;

在我的Update()函数中:

代码语言:javascript
运行
复制
    if(Input.GetAxisRaw("Mouse ScrollWheel") != 0f)
    {
        SwitchGunByMouse(Input.GetAxis("Mouse ScrollWheel"));

    }

下面是SwitchGun()方法,它可以很好地处理alpha键或快速切换键。

代码语言:javascript
运行
复制
public void SwitchGun(int gunNumber)
{
    if (cannotChangeWeapon) return;
    
    //if (currentGun == gunNumber) return;

    if (gunNumber < 0)
    {
        gunNumber = allGuns.Count - 1;
    }

    if (gunNumber > allGuns.Count - 1)
    {
        gunNumber = 0;
    }

    for (int i = 0; i < unlockedGuns.Count; i++)
    {
        if(i == gunNumber && unlockedGuns[i])
        {
            activeGun.gameObject.SetActive(false);
            activeGun = allGuns[gunNumber];
            activeGun.gameObject.SetActive(true);
            previousGun = currentGun;
            currentGun = gunNumber;
            UIController.instance.ammoText.text = "AMMO: " + activeGun.ammo;
        }
    }
}

这是被调用的方法,它不起作用,这是我最后一次尝试使用它的来源。

代码语言:javascript
运行
复制
public void SwitchGunByMouse(float direction)
{

    if (direction == 0f) return;

    int nextGun = -1;

    if (gunsAvailable.Count -1 > 0)
    {
        if (direction > 0f)
        {
            for(int i = 0; i < gunsAvailable.Count -1; i++)
            {
                if (currentGun == gunsAvailable[i].idNumber) continue;
                if (currentGun < gunsAvailable[i].idNumber)
                    nextGun = gunsAvailable[i].idNumber;
            }

            if (nextGun == -1)
                nextGun = 0;
        }

        if (direction < 0f)
        {
            //int currentPos = gunsAvailable.idNumber;

            nextGun = gunsAvailable[currentGun].idNumber -1;
            
            List<Gun> g = gunsAvailable;
            
            g.Reverse();

            for(int i = 0; i < g.Count - 1; i++)
            {
                if (currentGun == g[i].idNumber) continue;
                if (currentGun < g[i].idNumber)
                    nextGun = g[i].idNumber;
            }
            
            //Okay so if we still haven't found a gun it means the other gun has a high id than our current gun
            //so now we need to figure out how to cycle again

            if(nextGun == -1)
            {
                nextGun = 0;
            }

        }

        Debug.Log("NextGun: " + nextGun);

        if (nextGun > -1)
            SwitchGun(nextGun);
        else 
            Debug.Log("Something bad happened with SwitchGunByMouse()");

    }
    
}

最后:

代码语言:javascript
运行
复制
public bool WeaponPickup(string gunToAdd, Gun myGun)
{
    switch(gunToAdd)
    {
        case "Repeater":
            if(unlockedGuns[1] == true)
                return false;
            else
            {
                unlockedGuns[1] = true;
                myGun.isCollected = true;
                gunsAvailable.Add(allGuns[1]);
                return true;
            }
        case "Sniper":
            if (unlockedGuns[2] == true)
                return false;
            else
            {
                unlockedGuns[2] = true;
                myGun.isCollected = true;
                gunsAvailable.Add(allGuns[2]);
                return true;
            }
        case "Rocket Launcher":
            if (unlockedGuns[3] == true)
                return false;
            else
            {
                unlockedGuns[3] = true;
                myGun.isCollected = true;
                gunsAvailable.Add(allGuns[3]);

                return true;
            }
        default:
            Debug.Log("Weapon Pickup ran into a problem with gunToAdd not matching a predefined string.");
            break;
    }

    return false;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-27 00:09:56

这应该能起作用:

代码语言:javascript
运行
复制
    public void SwitchGunByMouse(float direction)
    {
        if (gunsAvailable.Count <= 1 || cannotChangeWeapon)
        {
            return;
        }

        int gunSwitchDirection = Mathf.Sign(direction);
        int gunCandidate = currentGun;
        bool isCorrect = false;

        do
        {
            gunCandidate += gunSwitchDirection;

            if (gunCandidate < 0)
            {
                gunCandidate = allGuns.Count -1;
            }
            else
            {
                gunCandidate %= allGuns.Count;
            }

            isCorrect = gunCandidate != currentGun && unlockedGuns[gunCandidate];
        }
        while (gunCandidate==currentGun || !unlockedGuns[gunCandidate]);

        SwitchGun(gunCandidate);
    }

编辑:

您还可以稍微清理一下SwitchGun方法:

代码语言:javascript
运行
复制
    public void SwitchGun(int gunNumber)
    {
        if (cannotChangeWeapon) return;

        //if (currentGun == gunNumber) return;

        if (gunNumber < 0)
        {
            gunNumber = allGuns.Count - 1;
        }
        else if (gunNumber > allGuns.Count - 1)
        {
            gunNumber = 0;
        }

        if (unlockedGuns[gunNumber])
        {
            activeGun.gameObject.SetActive(false);
            activeGun = allGuns[gunNumber];
            activeGun.gameObject.SetActive(true);
            previousGun = currentGun;
            currentGun = gunNumber;
            UIController.instance.ammoText.text = "AMMO: " + activeGun.ammo;
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63590214

复制
相关文章

相似问题

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