How to combine variables?
I'm building a 3D platformer, and I've got five raycasts from my character to check for the ground, and then to actually set the grounded variable, I'm checking for all of them using || like this
if (centerFloorCheck || frontFloorCheck || rearFloorCheck || leftFloorCheck ||| rightFloorCheck)
Is there any way to combine all of my raycast variables into one, so that if one is true, it returns true, so that I can just check for that single variable?
P.S. I don't just want to do this
if (center || front || rear || left || back)
}
anotherVariable = true;
} else {
anotherVariable = false;
}
Is there any good way to do this?
Comment
Best Answer
Answer by jdean300 · Aug 21, 2016 at 01:01 AM
Yes - use the boolean |= operator. a |= b
computes a || b
and assigns it to a
bool groundCheck = false;
groundCheck |= Physics.RayCast(...); //One RayCast
groundCheck |= Physics.RayCast(...); //another RayCast
groundCheck |= Physics.RayCast(...); //another RayCast
/*... as many as you want*/
if (groundCheck)
{
//atleast one of the RayCasts returned true
}
Awesome, thanks! This will definitely help with a lot of other things too.