问题描述
我有4种颜色.我要这样做,以使播放器不能连续两次出现相同的颜色.当玩家与某个对象碰撞时,会调用RandomColor()
.因此该功能在游戏中会被多次调用,有时玩家不会改变其颜色.
I have 4 colors. I want to make it so that the player can't be the same color 2 times in a row. When a player collides with an object, the RandomColor()
is called. So this function is called many times during the game and sometimes the player does not change his color.
using UnityEngine;
public class ColorManager : MonoBehaviour {
public SpriteRenderer player;
public string playerColor;
public Color[] colors = new Color[4];
private void Awake()
{
RandomColor();
}
public void RandomColor()
{
int index = Random.Range(0, 4);
switch (index)
{
case 0:
player.color = colors[0]; //colors[0] is orange
playerColor = "orange";
break;
case 1:
player.color = colors[1]; //colors[1] is pink
playerColor = "pink";
break;
case 2:
player.color = colors[2]; //colors[2] is blue
playerColor = "blue";
break;
case 3:
player.color = colors[3]; //colors[3] is purple
playerColor = "purple";
break;
}
}
}
尝试使用while循环,执行while循环,但是显然我做错了,因为有时我连续两次收到相同的颜色.谁能弄清楚并解释其工作方式/原因,那将是很棒的,因为我在这个问题上花了很多时间,而且我很好奇.
Tried using while loop, do while loop, but I'm obviously doing it wrong, since I receive the same color twice in a row sometimes. It would be great if anyone figures it out and explains how/why it works, because I spent a big chunk of time on the issue and I am very curious.
推荐答案
首先,您需要一个函数,该函数可以生成排除后的随机数.以下是我为此使用的内容:
First, you need a function that can generate a random number with exclusion. Below is what I use for that:
int RandomWithExclusion(int min, int max, int exclusion)
{
int result = UnityEngine.Random.Range(min, max - 1);
return (result < exclusion) ? result : result + 1;
}
每次调用它时,都需要将结果存储在一个全局变量中,以便在下次再次调用它时将其传递给exclusion
参数.
Each time you call it, you need to store the result in a global variable so that you will pass that to the exclusion
parameter next time you call it again.
我修改了该函数,以便您不必在每次调用该函数时都这样做.新的RandomWithExclusion
函数将为您做到这一点.
I modified the function so that you don't have to do that each time it is called. The new RandomWithExclusion
function will do that for you.
int excludeLastRandNum;
bool firstRun = true;
int RandomWithExclusion(int min, int max)
{
int result;
//Don't exclude if this is first run.
if (firstRun)
{
//Generate normal random number
result = UnityEngine.Random.Range(min, max);
excludeLastRandNum = result;
firstRun = false;
return result;
}
//Not first run, exclude last random number with -1 on the max
result = UnityEngine.Random.Range(min, max - 1);
//Apply +1 to the result to cancel out that -1 depending on the if statement
result = (result < excludeLastRandNum) ? result : result + 1;
excludeLastRandNum = result;
return result;
}
测试:
void Update()
{
Debug.Log(RandomWithExclusion(0, 4));
}
最后一个数字将永远不会出现在下一个函数调用中.
The last number will never appear in the next function call.
对于您的特定解决方案,只需替换
For your specific solution, simply replace
int index = Random.Range(0, 4);
使用
int index = RandomWithExclusion(0, 4);
这篇关于每次生成随机数,不包含最后一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!