- Home /
How can I do random choices
I am making a FNAF game, and I want the enemy to randomly choice one of three spots to go, but I want to make it so If one is already there choice a different spot. I have no idea how to start this. Please help me. Thanks!
hum idk how you script load the choose so, I cant put a example, there is the starting point, the random function:
Answer by tormentoarmagedoom · Jan 30, 2019 at 08:55 PM
Good day.
you need first to do some bool variable to know if a spot is available or there is an enemy. Then, do something like this:
public GameObject[] SpotsList; //Array with all spots
int RandomNumber = Random.Range(0,SpotsList.length); // A random number
while (SpotsList[RandomNumber].isNotAvailable) //That bool variable to know if there is an enemy
{
RandomNumber = Random.Range(0,SpotsList.length); // Calculate a new random number to see if the while stops and can continue
}
Destination = SpotsList[RandomNumber] // so this spot is available because isNotAvailable for his was false.
Then, or you have a script in each spot, with that bool variable, or you make another array here to mark when a spotlist is available or not.
Bye!
i think you'll need at least 4 bools, 1 for each specific position and then 1 final that handles whether or not they are able to go anywhere in the first place.
so public bool spawn = true; public bool spotOne = true; etc etc
then each time a spot is occupied you set it to false.
also, like torment said public GameObject[] Spotslist; is really good i personally use public Transform[] when i do something like this.
Your answer
Follow this Question
Related Questions
Semi-Random Or Engine 1 Answer
Is Random.Range() Really Maximally Inclusive? 4 Answers
My Script Wont work? 2 Answers
How to make Random.Range() step by a certain amount? 1 Answer