- Home /
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.
Answer by unity_ek98vnTRplGj8Q · Dec 18, 2019 at 09:01 PM
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[i].transform.position, nodes[i-1].transform.position);
}
}
void Update(){
float currentDistance = 0;
//Add up length of all nodes we have already reached
for(int i=1; i<=node; i++){
currentDistance += Vector3.Distance(nodes[i].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);
}
This worked with small edits to fit my project. Thank you mate. It's quite simple math but I was thinking this whole thing much harder. :)
Your answer
Follow this Question
Related Questions
EditorGUI add SortingLayer-like list to custom Editor 1 Answer
Unity Editor script in DLL 1 Answer
Event for loading component in the editor 2 Answers
Get the highlighted variable in the inspector 0 Answers
Perform action before script compilation 0 Answers