Reputation: 199
I'm trying to find a more elegant way to reduce my if statements down, what I have right now is a little messy and I reckon could be done better, any suggestions?
if(ship.position.y >= transform.position.y + yBound)
{
hitBounds = true;
}
if(ship.position.y <= transform.position.y - yBound)
{
hitBounds = true;
}
if(ship.position.x >= transform.position.x + xBound)
{
hitBounds = true;
}
if(ship.position.x <= transform.position.x - xBound)
{
hitBounds = true;
}
Thanks for your time!
UPDATE
if(Mathf.Abs(ship.position.x - transform.position.x) >= xBound || Mathf.Abs(ship.position.y - transform.position.y) >= yBound)
{
hitBounds = true;
}
worked a treat, thanks a million!
Upvotes: 0
Views: 60
Reputation: 294
since in all conditions, hitBounds is "true". You can use one if with || (OR) conditions. I hope all these conditions are independent. If they are dependent use AND betwwen them. Good luck!
Upvotes: 0
Reputation: 2629
hitBounds = Math.Abs(ship.position.x - transform.position.x) >= xBound || Math.Abs(ship.position.y - transform.position.y) >= yBound;
Upvotes: 4
Reputation: 837
how about no if statement?
hitBounds = (ship.position.y >= transform.position.y + yBound) || (ship.position.y <= transform.position.y - yBound) || (ship.position.x >= transform.position.x + xBound) || (ship.position.x <= transform.position.x - xBound);
Upvotes: 0