Calculating Velocity for a non physics object

Hi, AssemblyLinearVelocity works only for physics based objects and I need to calculate velocity for a part that is moving by constantly updating CFrames.

I also need to know which direction the part is going.

Any help is greatly appreciated.

1 Like

Hey, velocity is calculated based on the previous position and the current position of the object in time t. Putting this together you can just do:

local velocity = (PreviousPosition - CurrentPosition) / DeltaTime
PreviousPosition = CurrentPosition
2 Likes

You can achieve this by measuring the positional delta (rate of change) of the object.

Sample code:

local velocity = Vector3.zero
local lastPosition = object.Position
RunService.RenderStepped:Connect(function(dt)
  local delta = object.Position - lastPosition
  velocity = delta / dt
  lastPosition = object.Position
end)
1 Like

Your example seems to be working, however I am noticing a slight issue.

image

For some reason it always skips a frame.

1 Like

Well as I said, it’s just an example to help you get a rough idea on how to calculate velocity. It’s by no means an ideal implementation.

use PreSimulation to modify velocity so it is ready for the physics to use

1 Like

I am not modifying velocity im just trying to calculate it. Physics also do not apply here, as im updating position by CFrames.