- Home /
iTween: uneven looping with PutOnPath
I've got a train engine running along a track, which the player can speed up or slow down. The track is a loop made with the iTween visual path editor (i.e. the start and end nodes of the path are in the same location). Regardless of speed or node layout, however, the train always hiccups when it comes to the loop point. There's always a slight jerk in its facing and a small disruption to its continued movement, regardless of how many nodes I add to smooth out the curve.
Is there some aspect of PutOnPath I'm overlooking? Thank you for your time.
function FixedUpdate(){
iTween.PutOnPath(gameObject,iTweenPath.GetPath(activePath),value);
if (value < 1) {
value += speed;
} else {
value = 0;
}
}
Answer by robertbu · Sep 20, 2013 at 04:22 PM
There may be an issue with PutOnPath(), but I see one issue with your code. The chance that your code will land exactly on 0 is small plus you do your check before you've incremented 'value', so 'value' could be greater than 1.0 when passed to PutOnPath(). So recode like this:
function FixedUpdate(){
iTween.PutOnPath(gameObject,iTweenPath.GetPath(activePath),value);
value = (value + speed) % 1.0;
}
Note this assumes that the last point and the first point in your path have the same position.
That did it! I hadn't thought that so small a discrepancy would hit so hard, but that's the world of code and numbers, I suppose. That's a good use for modulo that I hadn't yet come across, either. Thanks a lot for opening my eyes a little further.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113847im_/https://answers.unity.com/themes/thub/images/avi.jpg)