- Home /
How do I make speed constant in between position nodes?
I'm creating a game similar to this game seen here: https://www.mathsisfun.com/games/railway-line.html
I currently have the train following the tracks based upon some nodes that I have setup. My question is how do I get the train to move at the same speed? The straight tracks contain two nodes for each direction while the curved tracks contain four nodes that rotate the train as it goes around the corner. Anyone have any advice that would give me some way to make the speed constant. Here's the code that currently makes the train move through the nodes.
for(int i = 0; i < targetNodes.Length; i++)
{
while(Vector3.Distance(this.transform.position, targetNodes[i].position) > .1f)
{
Transform childT = targetNodes[i];
transform.position = Vector3.Lerp(transform.position, childT.position,.05f);
transform.rotation = Quaternion.Lerp (transform.rotation, childT.rotation, .1f);
yield return new WaitForSeconds(.05f);
}
}
Note: the distance between the nodes is not the same hence while I'm have the issue. I know in physics one way to do this would be to take the unit vector and then multiple each component by a coordinate.
That use of Lerp is specially made to go fast then slow.
Don't let the word "lerp" confuse you. Lots of different formulas use lerp, the same way they use plus or multiply.
Answer by techboysquared1 · Jun 18, 2015 at 02:58 AM
Answer can be found here: http://answers.unity3d.com/questions/746532/vector3lerp-constant-speed-between-distance-change.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613201120im_/https://answers.unity.com/themes/thub/images/avi.jpg)