title: Moving objects back and forth
body: <p>I'm trying to move multiple objects, simultaneously from Point A to Point B and back again, looped to serve as obstacles in blocking the player.</p>
  
  <p>I've tried</p>
  
  <pre><code>StartCoroutine(Oscillate(OscillationFunction.Sine, 1f));
    public IEnumerator Oscillate(OscillationFunction method, float scalar)
    right = GameObject.FindGameObjectsWithTag("MovingObs2");
                  foreach (GameObject r in right)
                  {
                      startPos = r.transform.position;    
                      v = startPos;
                      v.x = (r.transform.position.x + (Mathf.Cos(Time.time) * scalar));
                      r.transform.position = v;
                      yield return new WaitForSeconds(2);
                      r.transform.position = startPos;
                  }
  </code></pre>
  
  <p>and others, but they're all difficult to contain within a desirable distance and speed from the starting position. It's too fast, and too far.</p>
  
  <p>and I tried a seemingly simpler line, which is easier to understand, for me.</p>
  
  <pre><code> v.x = l.transform.position.x + speed * Time.deltaTime;
              l.transform.position = v;
  </code></pre>
  
  <p>but since i'm using an array in a foreach loop, i don't know how to preserve each GameObjects' transform.position, so that it can be used as a condition to reverse the direction of the objects' movement every time it reaches either Point A, or point B.</p>
  
  <pre><code>   if (l.transform.position.x &lt;= startPos.x || l.transform.position.x &gt;= startPos.x + endPos.x)
          {
              speed *= -1;
          }
  </code></pre>
  
  <p>edit: I apologize if I asked a duplicate question, I thought it was different due to the number of objects involved in an array.</p>