# How to reverse this?

**URL:** <https://discussions.unity.com/t/how-to-reverse-this/495340>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [January 19, 2013, 11:42am UTC](https://discussions.unity.com/t/how-to-reverse-this/495340 "2013-01-19T11:42:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TylerPerry](https://avatars.discourse-cdn.com/v4/letter/t/45deac/32.png) [@TylerPerry](https://discussions.unity.com/u/TylerPerry)\
**Post date:** [January 19, 2013, 11:42am UTC](https://discussions.unity.com/t/how-to-reverse-this/495340/1 "2013-01-19T11:42:10Z")

</div>

So for my game I’m making a system to move platforms around, basically I have an array of way points that it moves to, but now I need it to when it gets to the end it goes in reverse(so like 123454321 and then start again) in an endless loop? but I have no idea on how I might do this \>.\<

```auto
#pragma strict
var transformArray : Transform [];
var speed : float = 10.0;
private var speed2 : float = 0.0;
private var index : int = 0;
private var i:float = 0.0;
private var rate:float = 0.0;
private var dist : float = 0.0;

function Start()
{
Initiate();
}
function Update () 
{ 
    MoveObject(this.transform, transformArray[index], transformArray[index +1], speed2);
}

function MoveObject (thisTransform : Transform, startPos : Transform, endPos : Transform, time : float) 
{
    rate = 1.0/time;
    if (i < 1.0)
    {
        i += Time.deltaTime * rate;
        thisTransform.position = Vector3.Lerp(startPos.position, endPos.position, i);
    }
    else if(index < transformArray.Length-2) 
    {
     index +=1;
     i = 0.0;
     Initiate();
    }
}

function Initiate()
{
  dist = Vector3.Distance(transformArray[index].position, transformArray[index +1].position);
  speed2 = dist * speed;
  Debug.Log ("" + speed2);
}

```

---

<div class="post-metadata">

**Author:** ![Dmitry-Pyalov](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dmitry-pyalov/32/185969_2.png) [@Dmitry-Pyalov](https://discussions.unity.com/u/Dmitry-Pyalov)\
**Post date:** [January 19, 2013, 12:52pm UTC](https://discussions.unity.com/t/how-to-reverse-this/495340/2 "2013-01-19T12:52:32Z")

</div>

If I understand you correctly, you just need to store start and end waypoints, then just use [Mathf.PingPong](http://docs.unity3d.com/Documentation/ScriptReference/Mathf.PingPong.html) to calculate the effective index of the new waypoint.
