I tried this : And the pingPong is working but after some times changing the pingpong flag true false many times i'm getting index out of bound exception :
The pingpong line that give the exception :
if(pingPong)
{
if(goForward)
{
transform.position = Vector3.Lerp(pos[index + 1]30], pos[index], Mathf.PingPong(Time.time, 1));
}
}
but first i'm getting exception out of bound index. second when making pingPongAnd how i don't want ittried to do the rest of the code onlyuse the pingPong. :
void Move()
{
if (pingPong)
{
transform.position = Vector3.Lerp(pos[index + 30], pos[index], Mathf.PingPong(Time.time, 1));
}
else
{
Vector3 newPos = transform.position;
float distanceToTravel = speed * Time.deltaTime;
bool stillTraveling = true;
while (stillTraveling)
{
if (changeDir && goForward)
{
goForward = false;
}
if (changeDir == false && goForward == false)
{
goForward = true;
}
Vector3 oldPos = newPos;
newPos = Vector3.MoveTowards(oldPos, pos[index], distanceToTravel);
distanceToTravel -= Vector3.Distance(newPos, oldPos);
if (newPos == pos[index]) // Vector3 comparison is approximate so this is ok
{
// when you hit a waypoint:
if (goForward)
{
atLastOne = index >= pos.Length - 1;
if (!atLastOne)
{
index++;
counter++;
if (counter == numofposbetweenpoints)
{
c++;
counter = 0;
}
if (c == curvedLinePoints.Count - 1)
{
c = 0;
}
}
else
{
if (stop && index == pos.Length - 1)
{
break;
}
else
{
if (loop)
{
index = 0;
}
else
{
index--;
goForward = false;
}
}
}
}
else
{ // going backwards:
bool atFirstOne = index <= 0;
if (!atFirstOne)
{
index--;
counter++;
if (counter == numofposbetweenpoints)
{
c++;
counter = 0;
}
if (c == curvedLinePoints.Count - 1)
{
c = 0;
}
}
else
{
if (stop && index == 0)
{
break;
}
else
{
if (loop)
{
index = pos.Length - 1;
}
else
{
index++;
goForward = true;
}
}
}
}
}
else
{
stillTraveling = false;
}
}
transform.position = newPos;
}
}
the logic when pingPong is true is to make the transform moving between the next waypoint and the last visited waypoint depending on the current moving direction if goFoward true or goFowards is false.The full script :