c# Random.Range won't stop

时间:2017-04-10 01:11:36

标签: c# unity3d

I'm writing a little text adventure for fun and I have a scene where the character is able to fish from a river. I'm trying to let him catch a few fish at a time but when I run the code the Random.Range method keeps going until it passes my threshold. I tried a few loops to see if I could get it to run once but no such luck.

I'm sure I'm missing something obvious but I keep running into the same problem. First I tried a for loop, the code below I used a do while loop but neither were effective. Thanks very much for your help.

Here's the code I'm having problems with:

public void catch_fish()
    {
        if (fish > 1000)
        {
            text.text = "Rock-Smasher already has a lot of fish! You should cook those first! \n\n " +
            "Press Space to back to the river";
            if (Input.GetKeyDown(KeyCode.Space))
            {
                currentState = States.river;
            }
        }
        else
        {
            int amount = 0;
            amount = Random.Range(0, 7);                
            fish = fish + amount;
            text.text = "You peer down into the water and you quickly thrust your hand into the water, catching " + fish + " fish. Wow! \n\n " +
                "Press R to go back to the river, or F to catch more fish";
            if (Input.GetKeyDown(KeyCode.R))
            {
                currentState = States.river;
            }
            else if (Input.GetKeyDown(KeyCode.F))
            {
                currentState = States.catch_fish;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

(代表OP发布)

我想使用随机数通常会因任何原因而继续运行,但我只是将RNG与Input.GetKeyDown方法捆绑在一起,并且它现在产生了预期的结果。