Download

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;

How accurate does this need to be…???

Might be tricky to get good numbers if players can fall off buildings / fall down holes with gravity effects…

Try something like Gbr said plus Curr-Position-Vector - Last-Position-Vector and Get-Distance-To node etc.

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?

Well, for starters you seem to be overwriting Distance Traveled each time etc…
Instead of adding to it at the end or keeping a Master-Distance-Travelled var.
But not a fan of any of this. Its ticking even if the player isn’t moving etc etc.

Wire an Input Axis -> Axis Value > 0 (That checks if player is moving + auto-ticks).
After that in a level, map out equally spaced markers in a line to test the accuracy.

Look at Level Editor Location. Default unit is CM. Space some stakes at 100 cm / 1 meter.
Then space incremental stakes at 10 cm intervals. Empirical work will help learn the units.
Have players walk between these markers → Then use print-string to ensure its working…

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.
1d75b85f3661279a7fcd10062458c1e382064785.png

You’re scaling the total distance traveled each time by a frame factor which is bogus!

Bug:
Look closely at the rightmost node in the exec pin chain its: SET Distance-Travelled.
You want total distance in that variable but its being tainted & overwritten each time.
To stop the confusion, you need either a Total-Distance var or Add node there instead.
Get rid the Add node & Distance Travled var where it is completely. Put after the Set!

Another Approach:
Also since this is an Auto-Runner. If the movement is constant you can guesstimate it.
Use a test map with markers at 1-meter ranges, how far do players get in 10 secs etc?
Account for Delta Seconds, then you can approximate player distance solely using time.
Using GetGameTime subtract old & new time to get implied distance then add to total.

(Also comments around nodes to try to articulate to yourself what each section does).

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?