- Home /
 
Moving previous waypoint with Lerp
Hello everyone,
I have an issue about Lerp and waypoints. I try to make my character move with waypoints. When moving forwards it works just fine; but I couldn't solve how to move previous waypoint. Can you help me about it?
Here is my code:
 using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class Move : MonoBehaviour
     {
     
         public Transform startPos, endPos;
         public Transform[] waypoints;
     
         private bool moveUp;
         private bool moveDown;
         public float speed = 1.0f;
         public float startTime;
         private float journeyLength;
         int currentStartPoint;
     
         private void Start()
         {
             currentStartPoint = 0;
             moveUp = false;
             moveDown = false;
             SetPoints();
     
           }
           void SetPoints()
           {
             startPos = waypoints[currentStartPoint];
             endPos = waypoints[currentStartPoint + 1];
             startTime = Time.time;
             journeyLength = Vector3.Distance(startPos.position, endPos.position);
           }
     
         public void PointerDownUp()
         {
             moveUp = true;
     
         }
         public void PointerUpUp()
         {
             moveUp = false;
     
         }
         public void PointerDownDown()
         {
             moveDown = true;
         }
         public void PointerUpDown()
         {
             moveDown = false;
         }
         void FixedUpdate()
         {
             float distCovered = (Time.time - startTime) * speed;
             float fractionOfJourney = distCovered / journeyLength;
     
             transform.position = Vector3.Lerp(startPos.position, endPos.position, fractionOfJourney);
     
             if (currentStartPoint == waypoints.Length - 1)
                  {
                      currentStartPoint = 0;
                  }
     
             if (moveUp)
             {
               if(fractionOfJourney >= 1f && currentStartPoint + 1 < waypoints.Length)
               {
                 currentStartPoint++;
                 SetPoints();
               }
     
             }
             else if (moveDown)
             {
              //I don't know how
     
               }
     
           }
 }
 
              
               Comment
              
 
               
              Your answer