I have a number of rays - exactly 125. Some must swipe up to down (boolean true), others down to up (so false). I would like to segment these gameobjects in order to get this :
I guess that there is something more efficient than the simple pseudo code below :
if (i => 0 && i <= 25) = true
if (i >= 26 && i <= 50) = false
if (i >= 51 && i <= 75) = true
if (i >= 76 && i <= 100) = false
if (i >= 101 && i <= 125) = true
...
How could you code this section? Maybe using a modulo? Thanks for your help ++
Your solution is elegant - and it works as expected. However I am trying to find a good function checking this integer as an argument. According to my first explanation, you used the integer 125. But what about another like 150 or 777 ?
I show you what I am trying to implement :
private void AdjustRays()
{
RaycastHit hit = new RaycastHit();
int ratio;
for (int i = 0; i < numOfRays; i++)
{ // simple brush up to down
if (feature == 1)
{
if (AdjustRayFromRaycast(rays[i].transform, horizontalScanAngle, scanAngle - Mathf.Sin(i), Mathf.PI * Random.Range(i / (float)numOfRays, i + 1 / (float)numOfRays), ref hit))
CreateDotFromRaycast(hit);
}
// simple brush down to up
if (feature == 2)
{
if (AdjustRayFromRaycast(rays[i].transform, horizontalScanAngle, -scanAngle + Mathf.Sin(i), Mathf.PI * Random.Range(i / (float)numOfRays, i + 1 / (float)numOfRays), ref hit))
CreateDotFromRaycast(hit);
}
// butterfly brush
if (feature == 3)
{
if (i % 2 == 0)
ratio = 1;
else
ratio = -1;
if (AdjustRayFromRaycast(rays[i].transform, horizontalScanAngle, (scanAngle * ratio) - Mathf.Sin(i), Mathf.PI * Random.Range(i / (float)numOfRays, i + 1 / (float)numOfRays), ref hit))
CreateDotFromRaycast(hit);
}
}
}
The first part displays these rays up to down, the second - down to up, and the third creates something like a butterfly effect. Playing with numOfRays, I am trying to create other effects - maybe something divided by 5 to get three section swiping up to down, and two - down to up ++
This sounds like an abuse of integer numbers to encode meaning. There are enums for that very purpose:
public enum Swipe {
UpToDown, DownToUp, Butterfly,
}
if (feature == Swipe.UpToDown)
else if (feature == Swipe.DownToUp)
else if (feature == Swipe.Butterfly)
Note: do not ignore the “else”. While this works without else it requires the code to check all three variants and if the conditions get more complex you may accidentally execute several branches at the same time.
And move the call to AdjustRayFromRaycast after the conditions. You only have the third parameter depend on the type of swipe, so you calculate that and put it in a variable and afterwards call the AdjustRayFromRaycast once to simplify the code and make it more resilient and easier to change.