Instantiating objects to form a triangle shape?

IEnumerator Live()
{
while (true)
{
float posY = 0.0f;
float posX = 2.0f;
Vector3 negXY = new Vector3(this.transform.position.x,
this.transform.position.y,
0.0f);
Vector3 posXY = new Vector3(this.transform.position.x,
this.transform.position.y,
0.0f);

			for (int i = 0; i < 1; ++i) 
			{
				Vector3 objectPosition = Vector3.Lerp(negXY, posXY, (float)i * (1.0f/Points));
				GameObject bCloneEP = Instantiate(Bullet, new Vector3(objectPosition.x,
				                                                      objectPosition.y - posY,
				                                                      objectPosition.z),
				                                  Quaternion.identity) as GameObject;
				posY -= 4;
				posX = -4;

				for (int ii = 0; ii < 3; ++ii) 
				{
					Vector3 objectPosition2 = Vector3.Lerp(negXY, posXY, (float)ii * (1.0f/Points));
					GameObject bCloneEP2 = Instantiate(Bullet, new Vector3(objectPosition.x + posX,
					                                                       objectPosition.y - posY,
					                                                       objectPosition.z),
					                                  Quaternion.identity) as GameObject;

					posX += 4.0f;
					if( ii == 3)
					{
						posX = -8.0f;
					}
				}

				for (int iii = 0; iii < 5; ++iii) 
				{
					posY = -8.0f;
					Vector3 objectPosition2 = Vector3.Lerp(negXY, posXY, (float)iii * (1.0f/Points));
					GameObject bCloneEP2 = Instantiate(Bullet, new Vector3(objectPosition.x + posX,
					                                                       objectPosition.y - posY,
					                                                       objectPosition.z),
					                                   Quaternion.identity) as GameObject;
					
					posX -= 4.0f;
				}
			}

Alright, well that works. But it’s painful to look at. I’ll edit it as I go along to make it done through a single loop instead of through multiple loops.

A for loop that tests floating point equality or inequality can easily become an infinite loop due to floating point imprecision.

For each line segment do a loop up to the count of the numbers of objects that must be instantiated per line segment (only counting one of the end two points). Calculate the position of each object by using Vector3.Lerp in between the two end points. Something like:

for (int i = 0; i < objectsPerSegment; ++i) {
   Vector3 objectPosition = Vector3.Lerp(segStartPos, segEndPos, (float)i * (1f/objectsPerSegment);
   // instantiate the object at that position
}