- Home /
How to write a "or "statement & a if Else with or condition?
Hi Guys,
I have 2 questions,
1) How do i insert a Or Random Range insert this function?
function ResetPosition ()
{
transform.position.y = 71;
transform.position.x = Random.Range(-11 ,-10))||(Random.Range(10, 11);
transform.position.z = 0;
rigidbody.velocity = Vector3.zero;
}
2) How do I add a or condition in the if else statement?
if (randomNo >5)
{
transform.position.x -= speed * Time.deltaTime;
if(transform.position.x < -6.1 )
{
//Instantiate(prefab, transform.position, Quaternion.identity);
transform.position.y = Random.Range(-2.3, 1.4);
transform.position. x = Random.Range (-5,5);
transform.position.z = -3;
}
}
else
{
transform.position.x += speed * Time.deltaTime;
if(transform.position.x > 6.1 )
{
//Instantiate(prefab, transform.position, Quaternion.identity);
transform.position.y = Random.Range(-2.3, 1.4);
transform.position. x = Random.Range (-5,5);
transform.position.z = -3;
}
}
I wish to combine <-6.1 & > 6.1 together.
Random.Range(10,11) always equals 10
Random.Range(-11,-10) always equals -11
Practically you did this:
transform.position.x = -11 || 10;
What do you wish to achieve?
Answer by gregzo · Apr 11, 2012 at 06:44 PM
Question 1)
I would do the following, altough I'm not 100% sure there is no more elegant way:
function ResetPosition ()
{
transform.position.y = 71;
if(Random.value<0.5)
{
transform.position.x = Random.Range(-11 ,-10);
}
else
{
transform.position.x = Random.Range(10, 11);
}
transform.position.z = 0;
rigidbody.velocity = Vector3.zero;
}
Answer by BiG · Apr 11, 2012 at 06:43 PM
1) It is not clear what you are trying to do here. Please explain.
2)
if(transform.position.x > 6.1 || transform.position.x < -6.1)
This way, as you planned, you can combine the two branches of your if-statement into a single one.
thanks! I wanted the enemies to randomly come from left & right screen.
So it's more like
transform.position.x = -7 || 7
transform.position.y = Random.Range(-2.3, 1.4)
But i'm curious if we can have one "or" || statement with 2 sets of random.range values. e.g
transform.position.x = Random.Range(-7,-4) || Random.Range(10,17)
what should to code be like with 2 sets of random.range in the "or" || statement?
O$$anonymous$$, in that case, I think that gregzo has already guessed what you meant. With a 50% probability, you "pick" a value in the range (-7,-4); with the other 50% probability, you "pick" a value in the other range (10,17). You don't need a "||" operator here: just look at gregzo's answer, for the first question of yours, and modify the ranges with respect of your last comment: it should work fine!