Help with a raycasting issue

Hello!! So I am using raycasting for a projectile, but it doesn’t end if a player walks into it and it ends too early if a player walks out (it’s made for rays so what did I expect). I was wondering if anyone had any way to achieve this or any alternatives to raycasting. Here’s a sample of my script:

local origin = myCharacter["Right Arm"].Position
			local direction = (mousePosition - origin).Unit * 200

			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = {
				workspace.TestPart,
				ProjectileClone,
				myCharacter
			}
			raycastParams.FilterType = Enum.RaycastFilterType.Exclude
			raycastParams.IgnoreWater = true

			local raycastResult = workspace:Raycast(origin, direction, raycastParams)
			local hitPos = raycastResult and raycastResult.Position or (origin + direction)

			local distance = (hitPos - origin).Magnitude
			local speed = 100
			local duration = distance / speed

			local tween = game:GetService("TweenService"):Create(
				ProjectileClone, 
				TweenInfo.new(duration, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),
				{Position = hitPos}
			)
			tween:Play()

As always, I appreciate any and all help from you guys. Thanks!

Other than mentioning the resource Fastcast

Do not tween, and every heartbeat frame raycast and update the visual projectile using the position += velocity * deltaTime formula.

how would I successfully implement this while still keeping the max range at 200 studs?

sooooo velocity by definition is distance / time.
doing: velocity * deltaTime is like:

(distance / time) * time which gives you just distance.
So you keep adding that value to some variable and check if it’s >= to 200.

If you want it to be a HARD LIMIT (i.e. do not go above 200 at any costs) then you can clamp the distance.

Can you please give me an example? Sorry, I’m not very good at understanding. :sob:

let’s say you want to tween in a specific direction.

local direction: Vector3 = someVector
local speed = 50
local MAX_DISTANCE = 200
local totalDistanceTravelled = 0

RunService.Heartbeat:Connect(function(deltaTime)
    -- insert tween stuff + raycast stuff here
    local distanceTravelled = deltaTime * speed
    if distanceTravelled + totalDistanceTravelled > 200 then
         distanceTravelled = 200 - totalDistanceTravelled
    end
    totalDistanceTravelled += distanceTravelled
    -- blah blah blah
end)

You can multiply direction by totalDistanceTravelled to get a position you want to tween to.