Hi guys i need some help, basically what i would like to achieve is to generate a random number (in this case 0 or 1) so the AI will choose what to do in a simple switch. The problem is that for how i did it, the AI will choose the action at the beginning and it will remain the same (indeed “rand” is randomized inside the Awake function). I would like to generate a random number every time the AIBehaviour function is called, I’ve already tried to put “rand = Random.Range(0, 2);” inside AIBehaviour first and then inside FixedUpdate, but it didn’t work. Is there a way to generate a random number multiple times?
Thanks for your time
Exactly this should work fine. What do you mean by “it didn’t work”? What happened? You don’t even need this to be a member variable at all. Just make it a local variable. Also the switch statement is a bit overcomplex for what you need.
void AIBehaviour()
{
int rand = Random.Range(0, 2);
if (rand == 0) {
m_aiRb.velocity = new Vector3(0.0f, 0.0f, m_aiSpeed);
m_aiAnim.SetBool("Run", true);
}
else {
m_aiAnim.SetBool("Run", false);
}
}
A problem here though is that you’re doing this in every FixedUpdate, which means it’s just going to very quickly switch between running and not running, probably too fast for any human to really see what’s going on properly.
Every time you call “rand = Random.Range(0, 2);” a new pseudorandom number is generated. So when you do it in your AIBehaviour method it should work as well. Since I see no code to “check” this why do you think it does not work? Note that Random class stores it’s state in a static field and thus shares it with other calls from all your code (you cannot have an instanceof it). So the generated number sequence for your AI instance may vary even when the seed is the same.
This is generally a quite useless statement in programming. You should specify in which way it did not work (does not compile, crashes the computer, gives an error/exception, gives results different than expected, destroys the universe). There is only one way for your code to work properly (as expected) but many ways to fail. To help find out in which it failed a detailed problem description is required.
Basically i’m recreating the first game (red light, green light) of the tv series “Squid Game”. In this game players can move only when the doll in front of them is turned towards the wall and is counting. When the doll finishes she looks at the players and anyone who moves is dead.
In this case i’m writing the code for the AI to run or stop randomly with a switch and a random number. I wrote “It didn’t work” because if i put “rand = Random.Range(0, 2);” inside the AIBehaviour function, every time i press play all the NPCs move and while they are moving they switch repeatedly between walk and idle animations.
Then you should triggert your AIBehavior method only when needed, not (almost) every frame like you do it now in FixedUpdate. You could have a timer counting down a random amount of seconds. Or check for conditions (other AIagents, their “senses”, events in the world etc.