- Home /
Random Values Seem Identical?
In my breakout clone I'm trying to split the ball, at which point they assume random directions to move in. This direction is decided in the Start() of each ball instance. Upon observation is seems that the more balls I split, the more obvious it becomes that all balls move in the same direction, towards the upper-right corner of the screen. Can anyone help? Why are the values the same every time the balls split? This is my code:
// Use this for initialization
void Start ()
{
ballRB = transform.rigidbody;
direction = new Vector3(0,1,0);
//If there's only one ball in the field, being this current one, the default up motion should be used.
if (GameObject.Find("StartBall").GetComponent<StartBall>().numBalls <= 1) ballRB.AddForce(direction*1000f, ForceMode.Force);
//If not, there are more balls, which means the player split the ball. A random direction should be used in this case.
else ballRB.AddForce(GenerateRandomDirection(), ForceMode.Force);
}
Vector2 GenerateRandomDirection()
{
int randX = Random.Range(0,360);
int randY = Random.Range(0,360);
randDirection = new Vector2(randX, randY);
return randDirection;
}
well you pick two non-negative random numbers, and make them into the components of a vector2... so the vector2 only ever has non-negative components of x and y. So the direction will only ever be in the direction of somewhere in the top right quadrant of a 2D graph (positive axes only). why not try using Random.insideUnitCircle ins$$anonymous$$d, you could normalize the reult if you wanted consistent speeds.
Answer by NoseKills · Jan 01, 2015 at 04:43 PM
It's because in Vector2 GenerateRandomDirection()
you are only assigning positive numbers to the x and y components of the force vector.
You are assigning them values from 0-360 which makes me think you are confusing vectors with angles.
If you add a force of Vector2(1, 1);
it will make the ball travel towards the positive end of world x- axis and y-axis (presumably top right). A Vector2(-1, -1);
would make it travel to the opposite direction (bottom left).
If you want the magnitude of the force (and thus resulting speed) to be equal for every ball, you should do something like
Vector2 GenerateRandomDirection()
{
float randX = Random.Range(-1f,1f);
float randY = Random.Range(-1f,1f);
// speed is a float variable to get the desired speed
randDirection = new Vector2(randX, randY).normalized * speed;
return randDirection;
}
EDIT @Scribe s solution with InsideUnitCircle is even better
Vector2 GenerateRandomDirection()
{
return Random.insideUnitCircle.normalized * speed;
}
Thank you, that solved the problem. I indeed put in the angles when I shouldn't have, stupid to not have looked carefully enough at the input.
So these vector values are always between -1 and 1, so I can use the Random.insideUnitCircle() function to gain a random number between -1 and 1? This solved my problem, but why not just use Random.range(-1,1) or something?
why not just use Random.range(-1,1) or something
I bet both of the ways presented in the answer do the same thing. The only difference is that in the latter one you have to write just 1 line of code yourself (`insideUnitCircle` returns a ready made vector2 for you, not just floats) so it makes your classes look prettier and cleaner :)
One thing to notice though... In both of these cases the returned vector can be (0, 0) in theory. $$anonymous$$aybe you'll have to do this just in case
Vector2 GenerateRandomDirection()
{
Vector2 rndv = Random.insideUnitCircle.normalized;
if (rndv == Vector2.zero)
{
rndv = Vector2.one;
}
return rndv * speed;
}
Thank you, that makes sense! Glad I'm understanding it. I'll add that Vector 0 exception, thanks. :)