- Home /
Choosing given numbers randomly
Hello my fellow Unity people
I got a question and need some help. So I am making a tetris like game to spawn blocks in a FPS like game.
As you can see in the picture my blocks are all over the place in the pit. That is because (as you will see in my code) I am spawning random values.
So my question is:
" How can I spawn these blocks Randomly between given values: like choosing -4, 0, 4 randomly instead of the numbers that are in between them."
I only want to spawn them randomly at the given values that I specify. That way it isn't all chaotic as you see in the picture. Show me the way! Here is my code:
public class GameController : MonoBehaviour {
public GameObject[] hazards;
public int hazardCount;
public float spawnWait;
public Vector3 spawnValues;
void Start()
{
StartCoroutine(SpawnBlock());
}
IEnumerator SpawnBlock()
{
for (int i = 0; i < hazardCount; i++)
{
GameObject hazard = hazards[Random.Range(0, hazards.Length)];
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, Random.Range(-spawnValues.z, spawnValues.z));
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
}
}
I thank you in advance
Use one or several Random.Range(0,1) then use three if()-statements to deter$$anonymous$$e which the final number should be. $$anonymous$$g. if(probablity < 2/3 && probability > 1/3){ randomSelection = -4;}
Answer by jdean300 · Feb 10, 2017 at 08:08 PM
Random.Range is overloaded to work with ints and floats. If you pass it floats, it gives you floats. If you pass ints, it will give you ints. So, just cast your spawnValues coordinates to ints:
Vector3 spawnPosition = new Vector3(Random.Range((int)-spawnValues.x, (int)spawnValues.x), spawnValues.y, Random.Range((int)-spawnValues.z, (int)spawnValues.z));
This may have some off-by-1 problems, so may need to add one to the result of the Random.Range call. Also note that Random.Range(int, int) is exclusive, meaning it will never return the max value, whereas Random.Range(float, float) in inclusive and can return the max value.
His problem is that he want to choose 3 arbitrary values though... Like -16, 1234214, and 198 with a uniform probability.
int[] options = {-16, 1234214, 198};
return options[Random.Range(0,3)];
Thanks ill try this where in the code would this go exactly so I don't screw it up. Just wanna make sure is all. Thank you
NEVER $$anonymous$$IND this worked perfect thank you so much I just plugged everything in myself. Basically put two and two together lol.
Here is the code just in case, am I right? Its working but I just want to know if there is a clearer method.
public int[] options = {-4, 0, 4};
public Vector3 spawnValues;
public Text scoreText;
public Text gameOverText;
private int score;
private bool gameOver;
void Start()
{
gameOver = false;
score = 0;
gameOverText.text = "";
UpdateScore();
StartCoroutine(SpawnBlock());
}
IEnumerator SpawnBlock()
{
for (int i = 0; i < hazardCount; i++)
{
GameObject hazard = hazards[Random.Range(0, hazards.Length)];
Vector3 spawnPosition = new Vector3(options[Random.Range(0, 3)], spawnValues.y, options[Random.Range(0, 3)]);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
}
That seems fine. The only thing that may be nice to change is to change Random.Range(0,3)
to Random.Range(0, options.Length)
. That way if you want to add more options all you have to do is add them to the array and everything else will still work. But if you are only going to ever have 3 options, it doesn't really matter.