- Home /
Randomly switching between 2 things
I'm making a simple game of red light green light and i need help with the switching of the lights. I've never worked with randomization so I'm at lost. i have a sprite sheet for the lights and the code below shows how i switch between rows in it. i want them to switch at random intervals as well. For example, green is up for 4 seconds then red for 2 seconds then green for 7 seconds. I don't know where to start and any example I've seen didn't explain anything. thank you ahead of time for your help.` AT = gameObject.GetComponent ("SpriteSheet") as SpriteSheet; AT.rowNumber = 1; //or AT.rowNumber = 0;`
Comment
Answer by sas_88 · May 13, 2015 at 08:51 AM
Add spritesheet to game object if condition satisfies
float timeframe = 0;
float first_val;
float sec_val;
void Update ()
{
if(timeframe==0)
{
first_val=Random.Range(0,7);
sec_val=Random.Range(first_val,12);
}
timeframe += 1 * Time.deltaTime;
if( timeframe>=(first_val-.2f)&&timeframe<=(first_val+.2f))
{
print ("Glow Green");
}
if( timeframe>=(sec_val-.2f)&&timeframe<=(sec_val+.2f))
{
print ("Glow Red");
timeframe=0;
}
}