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.
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
 local CurrentP = Attachment.WorldPosition

 if PreviousP ~= CurrentP then

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

    if RaycastResult then 

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


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


Well that was pretty easy apparently, thank you both.