Distance Traveled

Hello, how can i get the distance traveled by the player to show at a death splash screen?

If your game is a runner you can simply store the starting world location and subtract from the death world location, and then use the Vector Length node, which will return you a float value.

If its not linear, you will have to add in every tick the actual distance traveled in each frame. It will be something like:

Create a float TotalDistanceTraveled variable;

Event tick-> TotalDistanceTraveled += MovementComponent->Velocity->VectorLength * DeltaSeconds;

2 Likes

I’m using a very modified ue4 endless runner. i would like to do it in meters but not sure how i would get the unreal units then convert them to meters. that is one option i have thought might be possible

If you haven’t modified the size of UU (Unreal Units), each unit is 1 centimeter.

How would i get UU and convert them in BP?

You don’t need to, units are of the float data type.

Im pretty sure i messed up what you said to try. but it counts to about 14 then stays there and glitches out between 12 and 14. Did i do this wrong?

1 Like

Sorry i dont understand what you mean by this. When you check is axis is greater then 0 that connects to a branch. From the branch how do you determine when a stake is passed?

Have a Variable “Last Recorded Location”.
On BeginPlay, set this Variable to the ActorLocation.

On Event Tick, subtract the ActorLocation from the Last Recorded Location. The Length of the Difference Vector is your displacement in a Frame.
Simply add the Length to a float Variable “Distance Travelled”.

Doesn’t checking sqrt every frame get expensive?

It depends on how the runner moves. If it moves automatically, non-stoppable, the check won’t make any difference.

But if it needs inputs to move or can be stopped, then the check would make the implementation more elegant, indeed.

The problem is that the sum of DistanceTraveled must be AFTER the multiplication.

The check Frank has suggested you to add right after the Event Tick (correct me if I’m wrong) is:

It still Flickers the numbers to around 12-18. Also its a auto runner.

Could you show us a picture of the final blueprint?

Here it is i just want it to keep track of the distance then display it on death idk why im having so much trouble.

I moved it and it still does the same let me post a gif to show you. I dont really want to have it “Guess” what you are at i want it to know where you are at.
https://i.gyazo.com/0f01083be4594f055eaad596299f7b62.gif

You have to muliply Velocity.VectorLength with Delta and THEN add it to the original Distance. I recommend my approach with the lastRecorded Location tho. This is the most accurate one you can get.
Delta Time is not reliable, on a machine with massive lags you’ll get a huge error, although you ran the same distance.

How do you suggest i get the start and end location? on event play get controller location and then get the location before character is destroyed with distance in between?

That’s actually an idea I haven’t thought off (Poll the Distance once and not per Tick).
Yeah, you should set the StartLoc on BeginPlay (or whenever your Game Starts) and EndLoc on GameEnd (not after the Player is destroyed tho), get the difference from both Location and get The VectorLength. This is the total distance travelled.

Yeah i did that and it worked but i got one problem. It doesn’t feel like this is the right measurements. After running for like 3seconds its around 6,000 how can i have it increase slower?

Here is how it looks now with displaying the vector length but it increases at to fast of a rate.

Well, your velocity is too high I guess. Keep in mind that Unreal uses cm as a measuremeant ( 1uu = 1cm). If you have smaller measurements (your person is smaller than compared to real world), you need to adjust this of course. I suggest to scale everything to real world scale (1uu = 1cm)