Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to calculate how far an object has travelled?

Hello all! The question that has been going through my mind, is how to calculate how many studs an object has travelled. Now, I know the first thought in your head must be "Use magnitude between 2 objects!". Sadly, that's not the case. I'm trying to figure out the absolute distance, meaning overall. It could start from anywhere, and I need to figure out how many studs it has gone. Using magnitude wouldn't be the most efficient, as the object could turn around, and decrease the distance.

I'm well aware of how to figure out the SPS of an object at any given point in time, yet I don't know how to approach this. Tips?

1 answer

✓ Accepted answer

Add it up

while trackingThatDistance do
  local d = RenderStepped:wait() -- Or Heartbeat, or just wait() if you're checking on the server. 
  totalDistance = totalDistance + part.Velocity.magnitude*d
end

What this does is every time a new step happens for Lua (The closest I can get to a physics step), the distance that it would have travelled given its velocity is added on to the previous distance. This of course accumulates over time.

Why not check the difference in distance?
I considered it, and it's a plausible solution, but velocity may give a better measure in some cases. Ever had the ever-changing velocity but constant speed thing? Yeah, that's the problem at hand.

2 comments

0

How did you answer this so fast xD

0

Queen powers.

This discussion is preserved as it was. New answers, comments, and votes are closed.