- Home /
Pick between two floats
I want to be able to pick between either one of these values on start so that the value is the force applied.
I've tried this so far but all that gets returned is a 1 or a 0.
float[] choice = {-500f,500f};
void Start()
{
int result = Random.Range(0,choice.Length);
rigidbody2D.AddForce(new Vector2(0, result));
}
I'm guessing it's just picking out the list number rather than the value contained in that slot...
Can someone clear this up for me?
Comment
Best Answer
Answer by pacific00 · Jun 16, 2014 at 04:21 AM
try int result = choice[Random.Range(0,choice.Length)];
Decompose the lines to see where it fails but the line above should actually work.
int rand = Random.Range(0,choice.Length);
print(rand); // 0 or 1
int result = choices[rand];
print(result); // -500 or 500
This should work, though result should be a float, not an int.
Answer by robertbu · Jun 16, 2014 at 03:42 AM
You can do:
float result = (Random.value <= 0.5f) ? -500f : 500f;
rigidbody2D.AddForce(new Vector2(0, result));