Raycast detection is bad

When i hover my cursor over certain parts of a part the raycast wont detect it

here is the code

while true do
	if Equipt == true then
		print("RUNNING")
		if Mouse.Hit.Position then
			spawn(function()

				local RayParams = RaycastParams.new()
				RayParams.FilterType = Enum.RaycastFilterType.Exclude
				
				for i, v in pairs(game.Players:GetChildren()) do
					RayParams.FilterDescendantsInstances = {v.Character}
				end
				

				local raycast = workspace:Raycast(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, (Mouse.Hit.p - game.Players.LocalPlayer.Character.HumanoidRootPart.Position), RayParams)
				
				
				
				if raycast then
					
					local RayI = raycast.Instance
					print(RayI)
					local Outline = game.ReplicatedStorage.Blocks.Outline:Clone()


					local OutlinePosX = Snap(RayI.Position.X)
					local OutlinePosY = Snap(RayI.Position.Y)
					local OutlinePosZ = Snap(RayI.Position.Z)

					local SnapFrame = CFrame.new(OutlinePosX, OutlinePosY + 0.5, OutlinePosZ)

					Outline.Parent = workspace

					Outline:SetPrimaryPartCFrame(SnapFrame)

					wait(0.12)
					Outline:Destroy()
				end
			end)
		end

	end
	wait(0.1)
end
1 Like

i changed this line

local raycast = workspace:Raycast(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, (Mouse.Hit.p - game.Players.LocalPlayer.Character.HumanoidRootPart.Position), RayParams)

to

local raycast = workspace:Raycast(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, (Mouse.Hit.p - game.Players.LocalPlayer.Character.HumanoidRootPart.Position) * 18, RayParams)

i forgot to add the distance of raycast

1 Like

Raycasting is almost perfectly accurate, unfortunately there are many things you may of done wrong which could cause the issue.

Make sure your direction variable is correctly calculated and its distance is also correct
Along with the raycastparams, make sure its not hitting someone premature that may be in the way
Also I suggest not use mouse.hit and rather use the cameras cframe, but its really up to you

The second parameter to Raycast() is a vector that’s expected to be direction * distance. So in your original code, your raycast was exactly the distance to the hit point, which is why it was flickering (it was just barely reaching and sometimes not reaching).

Your modified version in the second post is raycasting 18 times the distance between the character and the part, not 18 studs! If your intent is for the reach of the character to be a specific distance, e.g. 18, what you want that vector to be is:

 (Mouse.Hit.p - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Unit * 18
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.