【问题标题】:Unbreakable Loop in Unity, not sure how to fix. Causing Unity to crashUnity 中的牢不可破的循环,不知道如何修复。导致 Unity 崩溃
【发布时间】:2019-04-29 10:50:51
【问题描述】:

我正在尝试创建一个统一的“组合”系统,但我创建了一个我难以理解的牢不可破的循环。我尝试过使用 while 循环并且遇到了这个问题,所以也想尝试一下 for 循环,但我得到了相同的结果。

连击系统的工作方式是,当玩家进入有敌人的条件时,他们可以输入控制器上的一连串按钮来触发连击。如果玩家输入了正确的组合,就会应用效果。到目前为止,我只担心让组合系统工作。

我为组合创建了一个预定义的字符数组,然后我将来自播放器的输入与该数组进行比较。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ComboSystem : MonoBehaviour
{
    public char currentButton;
    public char[] combo_arr = { 'A', 'A', 'B', 'B', 'X' };

    PlayerLockOnSystem plos;

    private void Start()
    {
        plos = GetComponent<PlayerLockOnSystem>();
    }

    private void Update()
    {
        if (plos.lockedOn)
        {
            Combo();
        }
    }

    void DetectComboButtons()
    {
        if (Input.GetButton("Joystick A"))
        {
            currentButton = 'A';
        }

        if (Input.GetButton("Joystick X"))
        {
            currentButton = 'X';
        }

        if (Input.GetButton("Joystick B"))
        {
            currentButton = 'B';
        }


    }

    void Combo()
    {



        for (int i = 0; i < combo_arr.Length; i++)
        {
            DetectComboButtons();

            if (currentButton == combo_arr[i])
            {

                Debug.Log("Correct: " + currentButton);
            }
            else
            {
                i = 0;
                Debug.Log("Incorrect");
            }
        }






    }




}

当触发 Combo() 方法时,Unity 崩溃,我必须强制关闭编辑器。

【问题讨论】:

  • 你需要给DetectComboButtons添加一个while循环和一个延迟。该代码不等待输入,它只是返回按下的键,如果没有按下任何键,则返回任何内容。您需要等待输入
  • 您在导致无限循环的循环中设置 i =0。您必须跟踪玩家当前的按钮按下以及他们所在组合的当前索引。如果下一个按钮不正确,则重置当前的组合索引。
  • 感谢您的快速回复,我仍然有点不确定您的意思,如果这是我没有完全了解的非常基本的事情,我很抱歉
  • 看看this guide是否有帮助。
  • 好的,我去看看!谢谢

标签: c# loops unity3d


【解决方案1】:

您需要在不同的更新调用中检查帧之间的输入。试试这个:

private const char CHAR_THAT_MEANS_THAT_THE_PLAYER_DIDNT_BREAK_THE_CHAIN = '0';//doesnt matter

private int _currentPlaceInTheComboChain = 0;

private void Update()
{
    if (plos.lockedOn)
    {
        Combo();
    }
}

char DetectComboButtons()
{
    if (Input.GetButtonDown("Joystick A"))
    {
        return 'A';
    }

    if (Input.GetButtonDown("Joystick X"))
    {
        return 'X';
    }

    if (Input.GetButtonDown("Joystick B"))
    {
        return 'B';
    }
 return CHAR_THAT_MEANS_THAT_THE_PLAYER_DIDNT_BREAK_THE_CHAIN;


}

void Combo()
{
    char currentButton = DetectComboButtons();
    if (currentButton == CHAR_THAT_MEANS_THAT_THE_PLAYER_DIDNT_BREAK_THE_CHAIN) 
    {
       return; //the player didn't continue the combo but didn't break it (yet)
    } 

    if (currentButton == combo_arr[_currentPlaceInTheComboChain])
    {
       _currentPlaceInTheComboChain++;//wait for the next button, will only be checked the next time update is called
       Debug.Log("Correct: " + currentButton);
       if (_currentPlaceInTheComboChain == combo_arr.Length) { //this was the last button in the combo
           _currentPlaceInTheComboChain = 0; //for a new combo
           Debug.Log("Combo completed");
       }
    } else {
      _currentPlaceInTheComboChain = 0; //player broke the chain
      Debug.Log("Incorrect " + currentButton);
    }

}

【讨论】:

  • 好消息,它不再运行无限循环,代码有点工作。当我按下正确的按钮时,控制台输出正确和错误,并且似乎没有进一步前进。
  • 你的意思是它只识别了第一个但没有继续,也没有达到“组合完成”,或者它一次继续一个按钮?
  • 它似乎没有前进,因为“组合完成”从未出现在我的日志中。这是我按下正确组合后得到的结果prntscr.com/ni9v4b
  • 哦,你需要使用 GetButtonDown 而不是 getButton 否则按下时间过长将被视为按下 2 次或更多次
  • 代码可能会简化一点(可能使用协程),但答案没有错。汤姆,您可能还想编辑答案以使用GetButtonDown
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多