Problem with raycast hitbox i'm trying to make

I want to make a simple raycast hitbox (I know the raycast module, I just wanna make my own for learning and experiment purposes) but i have a problem.

The problem is hitbox is totally off and makes no sense, it detects a part even if its far away from it, kinda hard to explain.

https://gyazo.com/f5f4404a25c7b08c6ca098e73158fc2a
This is the gif of it, and here is the code:

local Attachment = script.Parent.HitboxAttachment
local Debris = game:GetService("Debris")


while true do

 local PreviousP = Attachment.WorldPosition
 task.wait(0.03)
 local CurrentP = Attachment.WorldPosition

 if PreviousP ~= CurrentP then

 local RaycastResult = workspace:Raycast(PreviousP, (CurrentP-PreviousP).Unit)

    if RaycastResult then 
     print(RaycastResult.Instance.Name)
     --break
    end


    local Distance = (CurrentP-PreviousP).Magnitude
    local BeamCFrame = CFrame.new(CurrentP, PreviousP)
  
	local DebugPart = Instance.new("Part")
	DebugPart.BrickColor = BrickColor.new("Sea green")
	DebugPart.Material = Enum.Material.Neon
	DebugPart.Anchored = true
	DebugPart.CanCollide = false
    DebugPart.CanQuery = false
	DebugPart.Name = "DebugPart"
	
	DebugPart.Size = Vector3.new(0.1, 0.1, Distance)
	DebugPart.CFrame = BeamCFrame * CFrame.new(0, 0, -Distance/ 2)
	
	DebugPart.Parent = workspace.DebugParts
    Debris:AddItem(DebugPart,0.2)
 end
end

Workspace

And this is the workspace. I didn’t use studio for a long time so I’m pretty sure i did some math wrong or something like that but i can’t figure it out. Thank you.

Why .Unit it?

It will force the length to become one much greater than it should be when the length should be 0.5 studs or less.

I suggest removing the .Unit

1 Like

What can i replace it with, is there an alternative?

just remove the Unit

(CurrentP-PreviousP)

Well that was pretty easy apparently, thank you both.