Need a way to record distance traveled

I am trying to find a way to record the distance my player controlled character has traveled. I need this for some various functionalities in my project. I looking for something like a total distance travelled that is recorded anytime the character is not idle. I am not sure how to change the world position of the character to a linear distance traveled. Any help with this is greatly appreciated.

One solution is to calculate cumulative difference between your last location and current location.

ie create a variable in your Player BP named ‘LastLocation’ (Vector) and another varibale ‘TotalDistanceTravelled’ (float). Now within your tick event, you should do this:

TotalDistanceTravelled = TotalDistanceTravelled + Length(GetLocation() - LastLocation)

LastLocation = GetLocation()

And within your BeginPlay event, do:

LastLocation = GetLocation()

TotalDistanceTravelled = 0

Hope this helps.

Thanks, this is exactly what I was looking for. I appreciate the help.

If the issue is solved, dont forget to mark this thread as solved. Thanks

also keep in mind to not do it every tick…unless you are traveling zick zack at the speed of light its not nessessary to track movement xx times a second. Instead better use delta time (comes with event tick) to count up to one or a few seconds and then execute the answer above.

I’m looking to do the same thing but being still roughly new with blueprint I don’t exactly know how to go about translating what you have above into a working blueprint.

Adding the LastLocation and TotalDistanceTraveled variable, no problem. Could you possibly explain the setup a little more or maybe include a screenshot of this setup in blueprint?

Thanks!

Dont have my PC nearby, but i will try and post a pic as soon as possible (might take 24 hours)

Thank you very much, I appreciate it!

Use a function in combination with a looped “set timer” node. Set timer node runs the function you specify with a frequency you choose (e.g. every 3 seconds). You can call it after the begin play event. Just create a custom event called “Add Distance” and follow the example I attach:

Thanks so much for adding the image of the blueprint! Now the set of text above makes perfect sense once I’m able to see it visually represented! I will add this to my level later tonight and comment back when I get it working!

Thanks again!

This worked perfectly! Thanks again for adding the picture!

Sorry to bother you with another question but I couldn’t find a solution for this either. I have the distance working now and it is being called on screen by my UMG widget. However, it’s adding very large number increments rather than constantly adding one. I’m looking to do something like kind of in this tutorial video here: Endless Runner: Wrap Up | 07 | v4.7 Tutorial Series | Unreal Engine - YouTube

Thanks!

Please start a new thread for a new question. Locking this thread as the solution to the original question has already been accepted.

PS: If you want to increment the distance at a constant rate, the best place to do that would be in the widget’s tick or if you are using normal HUD, then within its Draw event. Instead of displaying the current value of total distance travelled, interpolate between the currently displayed value and the present value of TotalDistance travelled and display the result.

Apologies! Will start a new thread if I have any other issues! Thanks again!