Path Follower Problems

I created a PathFollower script to move an object along a set of nodes. It works mostly as intended but there are some quirks i could really use some help with:

  1. SOLVED The object doesnt reach the final node

  2. SOLVED My code for looping (from the last to first node) is very clumsy and isn’t dynamic

  3. It would be cool to control the amount of time the object spends at a node in the editor. Even better if it can be different from node to node.

The object will be a metro train in a 2D platform type game. The train arrives coming from the right. Stops and exits left. Then loops back outside of view in the bottom of the scene. I posted a video in my 2nd post showing this.

Thanks in advance for any help offered, the relevant scripts below:

using System.Collections;
using UnityEngine;
public class PathFollower : MonoBehaviour

The PathFollower Script
{
    Node[] PathNode;
    public GameObject MovingObject;
    public float MoveSpeed;
    float Timer;
    //holds current node position
    [SerializeField] int CurrentNode;
    // static
    [SerializeField] public static Vector3 CurrentPositionHolder;
 
    void Start()
    {
        //Get Node children (from hierarchy)
        PathNode = GetComponentsInChildren<Node>();
        CheckNode();
    }
    //function to check current node an move to it,
    //by saving the node position to CurrentPositionHolder
    void CheckNode()
    {
        if (CurrentNode < PathNode.Length - 1)
        {
            Timer = 0;
            // CurrentNode Position is put in CurrentPositionHolder
            CurrentPositionHolder = PathNode[CurrentNode].transform.position;
            Debug.Log(PathNode);
       
        }
        //Looping back
        if (CurrentNode == 4)
        {
            Debug.Log("Node = 4, let's go back");
            CurrentNode = -1;
        }
        //At the station (slower)
        if (CurrentNode == 1)
        {
            MoveSpeed = 0.01f;
            Debug.Log("Speed inside station");
        }
        //Outside station (faster)
        if (CurrentNode != 1)
        {
            MoveSpeed = 0.05f;
            Debug.Log("Speed outside station");
        }
    }
    // Drawing lines in editor
    void DrawLine()
    {
        for (int i = 0; i < PathNode.Length; i++)
        {
            if (i < PathNode.Length - 1)
            {
                Debug.DrawLine(PathNode[i].transform.position,
                    PathNode[i + 1].transform.position, Color.blue);
            }
        }
    }
    void Update()
    {
        DrawLine();
        Debug.Log(CurrentNode);
        // This makes the thing move
        Timer += Time.deltaTime * MoveSpeed;
        if(MovingObject.transform.position != CurrentPositionHolder)
        {
            // If MovingObject not at node, move to node
            MovingObject.transform.position =
                Vector3.Lerp(MovingObject.transform.position, CurrentPositionHolder, Timer);
        }
        else
        {
            if (CurrentNode < PathNode.Length - 1)
            {
                // If MovingObject is at Node, move to next
                CurrentNode++;
                CheckNode();
            }
        }
    }
}

A short look at what it looks like in the Unity Editor

So I managed to get it too reach the final node, also i got it to loop however long the array of nodes is. So that’s solved. However i can’t quite figure out where in the script is decided the metro pauses at the nodes. I’d like to get some control over those pause times.

Line 72 will keep it moving until every bitter end tiny billionth of a millimeter of motion is done and you are at the point (eg, checking for equality). In general, you shouldn’t check floating point numbers for equality. Here’s why:

https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

Lerping towards a goal moves a fraction of the remaining distance, which as you gets closer gets tinier and tinier.

This is why it slows down, causing it to move those final billionths of a pixel over some indeterminate amount of time.

Instead, either:

a) use Mathf.MoveTowards() to do the moving, or better yet:

b) install and use LeanTween or DOTween or iTween and let someone else write complicated moving code for you.

Then you just tell the tweening package “Hey, make this go to this point and tell me when you get there.”

Thanks man, that link you shared is interesting.
I’ll look more into this