How can I change the object to move on the waypoints to move also in reverse?

Now it’s moving from the first position the last position and then in the end it’s moving to the first position and then moving forward on all positions again and so on.

I want to use a flag bool variable that if it’s true when the object to move on the waypoints will get to the last position it will then travel backward reverse on the positions and when it will get to the first one it will start moving forward again and so on like a ping pong. and if the bool flag is false it will be like now.

I’m not sure how to handle the line :

index = (index + 1) % pos.Length; 

Not sure what the percentage % does in this line.

using System.Collections; using System.Collections.Generic; using UnityEngine;  public class MoveOnCurvedLine : MonoBehaviour {     public LineRenderer lineRenderer;     public GameObject objectToMove;     public float speed;     public bool go = false;     public bool moveToFirstPositionOnStart = false;      private Vector3[] positions;     private Vector3[] pos;     private int index = 0;      // Start is called before the first frame update     void Start()     {         pos = GetLinePointsInWorldSpace();          if (moveToFirstPositionOnStart == true)         {             objectToMove.transform.position = pos[index];         }     }      Vector3[] GetLinePointsInWorldSpace()     {         positions = new Vector3[lineRenderer.positionCount];         //Get the positions which are shown in the inspector          lineRenderer.GetPositions(positions);           //the points returned are in world space         return positions;     }      // Update is called once per frame     void Update()     {         if (go == true)         {             Move();         }     }      void Move()     {         Vector3 newPos = objectToMove.transform.position;         float distanceToTravel = speed * Time.deltaTime;          bool stillTraveling = true;         while (stillTraveling)         {             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             {                 index = (index + 1) % pos.Length;             }             else             {                 stillTraveling = false;             }         }          objectToMove.transform.position = newPos;     }    } 

I tried to do it this way :

using System; using System.Collections; using System.Collections.Generic; using UnityEngine;  public class MoveOnCurvedLine : MonoBehaviour {     public LineRenderer lineRenderer;     public GameObject objectToMove;     public float speed;     public bool go = false;     public bool moveToFirstPositionOnStart = false;      private Vector3[] positions;     private Vector3[] pos;     private int index = 0;     private bool isreverse = false;      // Start is called before the first frame update     void Start()     {         pos = GetLinePointsInWorldSpace();          if (moveToFirstPositionOnStart == true)         {             objectToMove.transform.position = pos[index];         }     }      Vector3[] GetLinePointsInWorldSpace()     {         positions = new Vector3[lineRenderer.positionCount];         //Get the positions which are shown in the inspector          lineRenderer.GetPositions(positions);           //the points returned are in world space         return positions;     }      // Update is called once per frame     void Update()     {         if (go == true)         {             Move();         }          if (index == pos.Length - 1)         {             System.Array.Reverse(pos);             isreverse = true;         }     }      void Move()     {         Vector3 newPos = objectToMove.transform.position;         float distanceToTravel = speed * Time.deltaTime;          bool stillTraveling = true;         while (stillTraveling)         {             Vector3 oldPos = newPos;             newPos = Vector3.MoveTowards(oldPos, pos[index], distanceToTravel);             distanceToTravel -= Vector3.Distance(newPos, oldPos);              if (isreverse)             {                 if (newPos == pos[index]) // Vector3 comparison is approximate so this is ok                 {                     index = (index - 1);// % pos.Length;                 }                 else                 {                     stillTraveling = false;                 }             }             else             {                 if (newPos == pos[index]) // Vector3 comparison is approximate so this is ok                 {                     index = (index + 1) % pos.Length;                 }                 else                 {                     stillTraveling = false;                 }             }         }          objectToMove.transform.position = newPos;     } } 

And it’s moving in reverse when it’s getting to the last position but there is a problem :

The first time it was moving in reverse fine but then when running the game over and over again it’s not moving in reverse and behaving like before. Not sure why. Why is that ?