- Home /
A single else statement for multiple if statements
Ok, I don't know if this is possible or not, and if this question has already been asked and answered, please link me to it cause I can't find it.
I want to make a single else statement to take the place of all these else statements
if(Vector3.Dot((playerObj.transform.position - transform.position).normalized, transform.TransformDirection(Vector3.forward)) > 0)
{
if(Mathf.Abs(playerObj.transform.position.x - transform.position.x) < awareDistStand)
{
if(playerObj.transform.position.y - transform.position.y < awareHeight)
{
var hitInfo : RaycastHit;
Debug.DrawRay(eyes.transform.position, transform.forward, Color.red, .1);
if(Physics.Raycast(eyes.transform.position, transform.forward, hitInfo, awareDistStand, layerMask))
{
if(hitInfo.collider.tag == "Player")
{
see = true;
}
else
see = false;
}
else
see = false;
}
else
see = false;
}
else
see = false;
}
else
see = false;
As you can tell, it would make the code much more elegant.
Thanks for the help!
Answer by snorcack · Sep 13, 2013 at 03:35 AM
I don't know how the logic around this code is implemented, but if you set "see" variable to false before the if statements, you should be able to avoid all of the else statements. This way "see" remains false if none of the conditions are met, and gets "true" when the innermost logic returns true. Though again I will say that I do not know the rest of the logic so it might need to be implemented in a different way
see = false;
if(Vector3.Dot((playerObj.transform.position - transform.position).normalized, transform.TransformDirection(Vector3.forward)) > 0)
{
if(Mathf.Abs(playerObj.transform.position.x - transform.position.x) < awareDistStand)
{
if(playerObj.transform.position.y - transform.position.y < awareHeight)
{
var hitInfo : RaycastHit;
Debug.DrawRay(eyes.transform.position, transform.forward, Color.red, .1);
if(Physics.Raycast(eyes.transform.position, transform.forward, hitInfo, awareDistStand, layerMask))
{
if(hitInfo.collider.tag == "Player")
{
see = true;
}
}
}
}
}
Answer by Eric5h5 · Sep 13, 2013 at 03:28 AM
No need for any else statements; just set "see" to false at the top of your code.
Your answer
Follow this Question
Related Questions
Problem with IF with 2 outputs. 1 Answer
OnTriggerEnter if else 2 Answers
How to do multiple 'if' statements for multiple different actions 4 Answers
Keeps returning True 2 Answers