- Home /
How to handle different bools in 1 statement
In different trigger i set variables to true or false. If the trigger is not hitting any, the unit should move.
In the update i want to test if those variables are false and move if so.
if(!in_range)
{
move();
}
works so far. But.
if(!in_range || !green_in_range)
{
move();
}
does not.
void move()
{
current_speed = speed;
transform.Translate (Vector3.right * current_speed);
}
The unit keeps moving. I wonder why. Am i using the wrong Operator? Thanks in advance.
Something isn't jiving.
Using a logical OR (||) is an early bail on a chain of evaluations of true in the condition expression. If !in_range evaluated to be true, then the second part(or the rest) of the logical OR in the expression would be skipped since you only need one in the series to be true. So this is the case of an either. If you want BOTH to be evaluated for true, you would use a logical AND (&&).
Answer by YoungDeveloper · Apr 15, 2015 at 01:14 PM
We can't see how you set up your booleans. Are your trying to check OR (||) or AND (&&)? Try this with debugs.
if(!in_range){
Debug.Log("in_range");
if(!green_in_range){
Debug.Log("green_in_range");
move();
}
}
I set them in the triggers.
void OnTriggerStay(Collider other)
{
if(other.tag == "red")
{
in_range = true;
}
if (other.tag == "green_feeler")
{
green_in_range = true;
}
void OnTriggerExit(Collider other)
{
in_range = false;
green_in_range = false;
}
and the set correct as i can see in the inspector.
I want to check if any of those bools is false. So in_range OR green_in_range. So the unit can move.
Your code piece excludes green_in_range when in_range is true, isn't it?
In my example, if in_range is false it will only go what's inside the brackets. Then it checks green_in_range, if it's false it will only then go and execute the debug on move. So at the end move will be executed only if both are false. Your Ontrigger is incorrect, you are setting both false even if only one exits it.
void OnTriggerEnter(Collider other){
if(other.tag == "red"){
in_range = true;
}
if (other.tag == "green_feeler"){
green_in_range = true;
}
void OnTriggerExit(Collider other){
if(other.tag == "red"){
in_range = false;
}
if (other.tag == "green_feeler"){
green_in_range = false;
}
}
The trigger exit seems to fix the problem, but still not with the operator (||). Only with your first piece of code. Any idea why? how would i handle 3 booleans? The same way?
In my example i use AND, because both must be false to trigger move(). In your example either one or other must be false to run move(). You still didn't said you need both of them false or just one.
Have you tried this?
if(!in_range && !green_in_range){
move();
}
I need both to check if they are false independent from the other. If in_range is true they should do attacking stuff. But not moving. If green_in_range is true they just wait for the unit in front to move or die. In_range is triggered when an enemy is in front. Green_in_range is triggered if a friendly unit is in front.
However i may need some more states to check like, if a build or base is in range. Thats why i asked if its possible to use more bools in your way.
Hope this makes sense. AND(&&) is not what i need. I need OR(||) (if any of this states become true - dont move)