How to find velocity of an attachment using GetVelocityAtPosition

All I am doing is trying to find the velocity of an attachment, while the part it is parented to rotates causing the attachment to move.

I am using GetVelocityAtPosition to do this, but when I print the vector3 it returns it is always 0,0,0, but
I can visibly see the attachment moving.

Here is my script:

local TS = game:GetService("TweenService")

local attachment = script.Parent
local part = attachment.Parent

wait(5)

local Tween = TS:Create(part, TweenInfo.new(5), {["CFrame"] = part.CFrame * CFrame.Angles(0, math.rad(180), 0)})
Tween:Play()

while wait(0.1) do
	print(part:GetVelocityAtPosition(attachment.WorldPosition))
end

That was just a way to test the rotation and velocity values in my real use case it is going to move irregularly, and without tween service, so don’t take the preset numbers or tween service into consideration.

If anyone could tell me what I am doing wrong or give me an alternative method to solving this problem it would be much appreciated.

Setting the CFrame isn’t the same thing as rotating. Setting the CFrame just updates the position and rotation of your object, it doesn’t actually invoke any velocity in your object. You will need to either do the math yourself to get the velocity at that point, or switch to something physics based instead of using CFrames.

Do you know how to do the math or a physics alternative?

Do you care about the exact vector, or just the magnitude of the velocity at that point?

I need both unfortunately. Now I will type this so I can get to the limit.

Is it always going to be rotating on that one axis? If so I can probably help. If it’s going to be rorating on more than the one axis, you’ll need to give me a week or so to remember how to work it while you wait for someone else to solve it first. In the meantime you can also just calculate the change in position every frame, which honestly isn’t elegant but it’s not the most inefficient.

So if I only need the magnitude of the velocity I could just measure the difference in position and divide it by time, but I do need to know which direction it is moving and it will move irregularly on all axis. I thank you for your help if you don’t have the time I understand.

1 Like

Because you are directly setting the CFrame, you already know the velocity, If your tween is linear then it’s just the degrees/time so your part rotating 180/5 or 36 degrees per second. Then get the distance from the attachment to the origin of the part. Then calculate the length of an arc with a radius of that distance and the degree of 36

For non-linear easing styles then you will need to find the function for each easing style you use

local TS = game:GetService("TweenService")

local attachment = script.Parent
local part = attachment.Parent

wait(5)

local Time = 5
local Degrees = CFrame.Angles(0, math.rad(180), 0)

local Tween = TS:Create(part, TweenInfo.new(Time), {["CFrame"] = part.CFrame * Degrees })
Tween:Play()

while true do
    task.wait(0.1)
    local DOA = math.sqrt(Degrees.X^2 + Degrees.Y^2 + Degrees.Z^2).  -- Degree Of Arc
    local LOA =  attachment.Position.Magnitude -- Length Of Arc
    local VelocityMagnitude = LOA*DOA
end

That was just a way to test the rotation and velocity values in my real use case it is going to move irregularly, and without tween service.

Well, then what I was writing is useless lol. The only thing I can think of is what @JarodOfOrbiter said and compare its position on every frame to its position on the prior frame. Although, If you arent using tween service are you going to be using physics constraints or just manually settings its position?