-2
\$\begingroup\$
foreach (Vector3 posit in position){
    if (ang){

        float a = Random.Range (0.0f, 60.0f);
        //Vector3 p1= new Vector3((position[i-1].x)+(ra*cos(a)),0.0f,(position[i-1].z)+(ra*Sin(a)));


    //  position.Add(p1);



    }
    else
    {
        //Quaternion.Angle=60.0f;
        float a = Random.Range (120.0f, 290.0f);


    }
    Vector3 p1 = new Vector3((posit[i-1].x)+(ra*(Mathf cos(a))), posit[i].y,(posit[i-1].z)+(ra*(Mathf Sin(a))));


    position.Add(p1);

    ang=!ang;
    //return(pos);
    i++;
}

return(position[i]);

}

\$\endgroup\$
1
4
\$\begingroup\$

Instead of foreach, you'll want to use for.

for (int i = 0; i < position.Count; i++) {
    Vector3 posit = position[i];
    ...
}

This way, you'll be able to access the previous position using position[i-1].

Keep in mind, however, that this won't work if i == 0 because you can't go less than zero, so you'll want to add some checks to ensure i > 0 before using position[i-1].

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.