Raycast duration calculation

Hello, I’m making a raycast beam type of thing but I don’t know how I would calculate the duration, for example, if the raycast hit far away it’ll still try to tween there in one second, the same time as if it would’ve hit a few studs away. I have looked everywhere and found nothing about this, can anybody help?

You can make your own arbitrary conversion for studs to seconds in terms of a tween time based on the distance.

local tweenTime = distance/50

and use that tweenTime variable in your Tween.

So if your distance was 325 studs, it would take 6.5 seconds to tween to it. Whereas if the distance was 25 studs, it’d only take half a second.

1 Like

First you have to have the distance between the 2 points, which is Magnitude

local Result = workspace:Raycast(Origin, Direction, Params)
local Magnitude = (Result.Position - Origin).Magnitude

with that you make rules of 3

local Time = Magnitude * 2	-- 2 = how long does it take to travel a stud

Ops, someone said it before

1 Like

Thank you both for your answers, I would love to mark both as solutions if I could.

The distance is Origin - Hit right?

Yes, the distance would be the origin (where the projectile came from) - the hit position.

1 Like