Skip to content

Instantly share code, notes, and snippets.

@nastajus
Created April 19, 2015 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nastajus/87cf2ec58c2f0e5ac1f5 to your computer and use it in GitHub Desktop.
Save nastajus/87cf2ec58c2f0e5ac1f5 to your computer and use it in GitHub Desktop.
nested ifs are the devil. NO! Do flat non-nested ifs instead, by reversing the if-statement negation and exiting instead.
//is called recursively to draw the beam of light
public void SetPoint(Vector3 origin, Vector3 direction)
{
RaycastHit2D hit = Physics2D.Raycast(origin, direction);
//hit something!
if (hit.collider != null)
{
if (hit.collider.GetComponent<Switch>().passable == true )
{
switch (hit.collider.tag)
{
case "Solid":
list.Add(hit.point);
break;
case "Reflect":
list.Add(hit.point);
Debug.Log("Re flect!!");
break;
case "Refract":
list.Add(hit.point);
Debug.Log("RE FRACT");
break;
case "Switch":
list.Add(hit.point);
Debug.Log(hit.collider.name);
break;
default:
break;
}
isOutsideScreen = false;
}
//hit nothing!
else
{
list.Add(origin + direction * Screen.width);
isOutsideScreen = true;
}
}
}
//is called recursively to draw the beam of light
public void SetPoint(Vector3 origin, Vector3 direction)
{
RaycastHit2D hit = Physics2D.Raycast(origin, direction);
//hit nothing!
if (hit.collider == null)
{
list.Add(origin + direction * Screen.width);
isOutsideScreen = true;
return;
}
//hit somethign
else
{
switch (hit.collider.tag)
{
case "Solid":
list.Add(hit.point);
break;
case "Reflect":
list.Add(hit.point);
Debug.Log("Re flect!!");
break;
case "Refract":
list.Add(hit.point);
Debug.Log("RE FRACT");
break;
case "Switch":
list.Add(hit.point);
Debug.Log(hit.collider.name);
break;
default:
break;
}
isOutsideScreen = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment