Raycast Doesn't Detect Attachments

I need to be able to click on a visible attachment in my game, and I’ve decided to use a raycast for it.

My code:

local function MouseSelectPreview()
	local mouseLocation = UserInputService:GetMouseLocation()
	local unitRay = camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
	local cast = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, castParams)
	if cast then
		print(cast.Instance) -- I expect to see the attachment here, but instead I see whatever is behind the attachment
	end
end
RunService:BindToRenderStep("MouseSelect", Enum.RenderPriority.Camera.Value, 

Thanks!

1 Like

I think this is the problem, you don’t actually need to use the ViewportPointToRay method. You can just do

local direction = game.Players.LocalPlayer:GetMouse().Hit.Position - workspace.Attachment.WorldCFrame.Position

If the direction doesn’t work try doing it the other way round, or multiply the direction by a bit more so it can reach further.

1 Like

This behavior is correct. RaycastResult.Instance is either a BasePart or a Terrain cell.

2 Likes

Hi. Attachments dont have collisions and aren’t able to be hit by raycasts. Maybe place another (invisible?) part at the same position as the attachment, with canCollide set to false and canTouch set to true.

2 Likes

oh? which means attachments can’t be detected by raycasts? aww

1 Like

yeah, that’s what I was thinking too. Which means I need to complicate my hierarchy, but that’s okay for the sake of it working (I’ll need weld constraints and other things). If I don’t get a solution in the next 10 minutes, I’ll mark your post as the solution :sunglasses:

2 Likes

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