Randomly set an integer as positive or negative?
So I'm working on a simple shooter, and I have a speed integer for my hazards. I would like to have the script randomly set the speed integer as positive or negative when the hazard first spawns, so that the hazards will move in different directions (I'm using transform.forward and transform.right * Speed for this). How would I have the speed value do this?
Answer by mathdev · May 29, 2017 at 12:23 PM
You can multiply your integer by the following code, which randomly returns -1 or 1
Random.Range(0,2)*2-1
as per @Bunny83 's answer: http://answers.unity3d.com/questions/597658/getting-1-or-1-randomly-without-including-the-0.html
Your answer
Follow this Question
Related Questions
.rotation doesn't seem to work, can't shoot projectile at mouse coordinates 0 Answers
A more optimized way to adjust duplicate integer values In a list? 0 Answers
why did the script change 0 Answers
Input Command Issues 0 Answers
Vector2 always becomes (1, 1), even if manully set to (0, 0)? 0 Answers