How to adjut this code to set it to a fix movement speed (spline/timeline)

Hello!

I got this code from doublecrossgaming (Ue4 Tutorial - Moving an Object along a path using a Spline Track - YouTube) and for my project its working really nice except one thing, the speed.
You can adjust the speed of the car by adjusting the “Duration” value. The problem is, the speed depends on the spline track lengh. So the bigger the track gets, the faster the car will move. I need the car to move at a set speed though, independent of the track lengh.

How could I adjust this code to make this happen?

The truth is that I dont understand this code, which is really bad. But it works so nicely that I kinda dont want to replace it with my own halfass code.So if you have a great day and want to make another dude happy, maybe you have some additional explaining for me…

Big thanks for your time, here is a potato for you

The code:

Easiest way is to set the timeline speed to be a multiple of the length of the spline. IE if the spline is twice as long, the TL should play at half speed.

Hello Clock, is your mentioned timeline speed the playrate or the alpha comming from the timeline?

Really trying to understand this… So the Alpha Value from the timeline seems to be a number between 0 and 1 while constantly growing until it reaches 1 then starts again.
This grow effect happens to be faster and faster as I add more and more to the spline, therefore the car moves quicker and quicker.
But why does the alpha value gets even affected by the spline length?

Here is a short video, just to make sure we all understand whats going on :smiley:
[video]TrackSpeed.mp4 - Google Drive

In general SPEED = LENGHT / TIME.

In your case Speed = SplineLength / Duration. Or better **Duration = SplineLength / Speed **

Then you should make a variable **float Speed **for your Actor class, that you can edit from Editor.

Inside the graph, you have to set **Duration **variable as SplineLenght /Speed at Event BeginPlay
In that way you set Speed for you actor and the duration is adaptive based on the desired Speed.

S.

It’s very straightforward.

You have the timeline on loop, that’s what it’s repeating.

The alpha goes from 0-1 and that’s what powers the lerp to go from the start to the end of the spline.

Instead of fiddling with the timeline every time you fiddle with the spline, all you have to do is use that SetPlayRate you already have in there to make the timeline play at a different rate. I can see they must have mentioned this in the tutorial, that’s why you have that duration variable on the play rate.

So all you have to do is change the duration variable…

From 0 to 1, so 0 is the start of the track and 1 the end, basically.
By setting the duration value to 1 the timeline takes 1 second to finish. So the car drives from start to end within 1 second.
Thats why it’s getting faster the bigger the tracks get.

Therefore I want to decrease the duration value everytime a new track was added.

So far correct?

So Duration = Duration / Spline Length?

Duration - is the duration. The longer you want it to last, the bigger you make duration.

You see, the great thing about timelines is they always execute within the specified time, that’s what you want.

But as you make the track longer and longer, in order to finish in the specified time, it has to move the car faster and faster.

You compensate for this by increasing the Duration variable, that’s why it’s there in the tutorial :slight_smile:

You can also just stick a fixed amount into that pin on the SetPlayRate. Set the spline up how you want and then decide how long you think it should take the car to move, then use the SetPlayRate to slow down that 1 second to the right amount.

Let’s say you’ve made the whole track and you think it should take 30 seconds, then you need to put .0333 into the pin, because that’s 1/30.

May I ask for your calculation formula then, please? Because I cant get it to work. I multiplied and divided everything I could think of and either its not moving at all or its eventually increased the speed. Spline Length = Duration is not working either. So is Spline Length minus 1 or 2. Dont know what else I could try.

The Spline starts with 2 Points and 30 Spline length.
As a new track gets added, 1 Spline Point and 100 Spline length gets added.

The fixed amount is not an option, because tracks can be added ingame.

Maybe the issue is that the player builds additional tracks while the timeline is running?

Edit: Do I have to connect “Finished” to “Set Play Rate” ? And thats why none of this was working?

The actual length of the spline, not the number of points:

length.JPG

It’s like this:

Set it up so that you Duration is set to 1 and fiddle with your spline until you’re happy with the speed the car goes. It will probably quite a short spline at the moment.

You find out how long your spline actually is like this:

Let’s say it’s 100. I don’t know it might be 5000 or 3, it doesn’t matter, but let’s say it’s 100.

So you’re happy with your car going 100 units in 1 second.

Then add some more points to your spline. It’s now longer.

While the game is running now, you can calculate the Duration variable like this:

( see the 100 in there, that’s the 100 from above, but in your case it will be another number ).

Hello Clock, thanks for your continues support. I made all my tracks/splines actually 100 units, so thats perfect.
I tried it already and this is the best result from all, even if it gets slower and slower. But not as noticeable as it was increasing with all my tries before and it might be due to the start duration value.
I might be able to tweak this, but it also might be due to this: I noticed that the playrate wasnt updating before. So where do I place the code of the update of the duration value and the playrate? Withing the “update” loop of the timeline?

Really appreciate your time you set into this man.

No worries :slight_smile:

You need to call that custom event to set the play rate at least at begin play. After that, you only need to call it when you change the length of the spline.

Hello Clock,

back on this project I noticed that one piece of the track, the curve, is shorter than the straigt one (the spline, not the mesh - obviously, did not came into my mind before, sorry)
I assume that the speed difference while adding and adding tracks ingame comes from this.

So instead of having a set value of 100 I do the following:
Get spline length before updating the spline = Old Spline Length
Get spline length after updating the spline = New Spline Length
New Length minus Old Length = Added Spline Length.

So I still use your formula but instead of 100 I use the value added Spline Length.
In my head this should work for both the straight track part and the curve part. But it feels that the car speed is still not constant.

Any last idea? Do I need different calcs when adding a straight part and a curve?

Hi - sorry, best if you quote people, then they get a notification.

I tried to find a way to automate this for you, but there isn’t one. It’s because you’re using a timeline.

So - here’s my offer. I’ll show you how to do it with tick. If you use tick, once you’ve set it up, you wont have to every touch the speed after editing the spline.

Sound good?

Here we go. Change your BP that moves the cart to be like this:

And you make a function to calculate the spline distance like this:

( you’ll need to tick the pure box in the details ).

Two variables, speed and distance. You don’t need to touch distance, just make the speed variable visible in the viewport and you can tinker from there.

EDIT: Better version of the BP: