How do I get the speed of an object?

Well, you might think that there that is no issue, but there is.
I’m making a simple open door using tween.
I added spark emitters, and here is what I am wanting to achieve.
The Rate of the spark emitters would be determined by the speed of the doors. The doors aren’t like your home door, it’s different.
The Issue is I can’t find the exact speed of the doors.
The only thing I can find is the position. They both are moving on the X-axis from their position.
Velocity and magnitude are not giving values, like any.
I can’t get the emitters to operate since I cannot find the speed of how fast the doors are closing/opening.
Any solutions would be nice.

3 Likes

Velocity has a magnitude of 0 (no movement) because:

Velocity is only relevant (updated automatically) when the part is being moved with physics.

velocity = distance over time

so,

velocity = (newPosition - lastPosition) / timeSinceLastPosition

If you wanted the rotation velocity, which you probably do, you’d do something like

rotVelocity = (newRotation - lastRotation) / timeSinceLastRotation

You could assign those variables every second, every frame, every time the tween updates, etc. Up to you.

5 Likes

You are saying that I need to assign variables in order to get the speed of the object?

You do.

30 char limit sucks for real

1 Like

If you’re not using physics to move an object, you won’t be able to read the Velocity property of the object, so yes, if you want the velocity, you need to calculate it yourself.

1 Like

I will try to use a formula that would be newPosition - LastPosition but only using the Position X axis.
I think that would determine the speed.

What? You would have to do all axis for it to get you the speed…

local lastPos = part.Pos
while true do
    wait()
    local velocity = (part.Pos - lastPos) / delta
    lastPos = part.Pos
    print(velocity.Magnitude)
end

Since they are moving on the X axis by position, you can get the speed because your doing subtraction of what the door is supposed to be on. Like if the door was supposed to be closed but it’s in the process of doing that, you can do the current position - the position it is trying to meet and then you will get your speed.
There is no other way to find the speed unless roblox physics is moving it.

Since I am using tween, the velocity will never update because roblox physics is not updating the velocity.
Thus, subtracting it via the X axis which is the only thing updating will get my speed.

Since I am using tween, the velocity will never update because roblox physics is not updating the velocity.

Yes, that is why I quite clearly defined velocity as a lua variable not part.Velocity

Since they are moving on the X axis by position, you can get the speed because your doing subtraction of what the door is supposed to be on.

Its best practice to do local velocity = (part.Pos - lastPos) / delta

What happens if the door stops moving?
Using the rest of the position will not give me what I am requiring.

If the door stops moving its velocity it 0.
And also as far as I can tell it will.

1 Like

When would delta be updated, and what and how would you set it to (number)?