# Move slider as moving object moves between nodes

Hey,

I’m trying to create HorizontalSlider in Unity editor.

Path contains any number of nodes and moving object goes through them from first to last.

I would need the HorizontalSlider value to move smoothly from 0 to 1 as moving object goes through it’s path from start to the end. So basically the moving object should reflect it’s position to the slider value.

Right now the slider jumps straight to the next node value. (0 %, 20%, 80%, 100% etc…)

`GUILayout.HorizontalSlider(node, 0, nodes.Count - 1);`

(EXAMPLE) Moving object at about 15% of it’s whole path so the slider should be exactly the same 15% of it’s path.

Hopefully the picture below helps to understand what I’m trying to currently achieve. Thanks in advance.

Try calculating the total path length on start like this (I have not tested this code)

``````List<Node> nodes;
float totalLength;
int node; //The last node you succesfully reached

void Start(){
//Find the total length of the path
for (int i=1;i<nodes.count;i++){
totalLength+= Vector3.Distance(nodes*.transform.position, nodes[i-1].transform.position);*
``````

}
}

void Update(){
float currentDistance = 0;

for(int i=1; i<=node; i++){
currentDistance += Vector3.Distance(nodes*.transform.position, nodes[i-1].transform.position);*
}
//Add the intermediate distance from the last node we reached
currentDistance += Vector3.Distance(transform.position, nodes[node].transform.position);

GUILayout.HorizontalSlider(currentDistance/totalLength, 0, 1);
}