Spacing based on loop index(middle, then right, then left)

I’m not sure the correct term but I’m looking to simply place an object’s position based on the count of the loop.
The spacing here being the X axis.

0: Middle

  1. Right/ + 5f
  2. Left/ - 5f;
  3. Right/ + 10f;
  4. Left /- 10f;

An attempt:

        int loopIndex = 0;
        foreach(EnemyData i in enemiesInCounter){
            int modifier = 5 * loopIndex;
            if(loopIndex % 2==0){
                modifier = -5 * loopIndex;
            }
          Debug.Log(Vector2.right * modifier);
            Vector2 position = Vector2.right * modifier + (Vector2.up * 2.5f);
            SpawnEnemyFromData(i,position);
            loopIndex++;
        }

The problem here is that loop count 2 is set to -10f instead of 5f;

I love this stuff… here’s my off-the-cuff attempt… print it out… I think it’s correct!

for (int i = 0; i < 11; i++)
{
  int index = (i + 1) / 2;
  int sign = (i % 2) * 2 - 1;
  int result = index * 5 * sign;

  Debug.Log( "i = " + i + ", result = " + result);
}

Magic is lines 3 to 5: accepts i and returns result.

2 Likes

Works like a charm, I’ll take some time and what everything means lol. Thanks alot!

1 Like