Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Raycasting not detecting parts?

Raycasting will occasionally not detect that it hit a part.

This script should print a position and a part's name, but randomly the ray doesn't say that it hit a part.
serverside:

game.ReplicatedStorage.OnShootRemote.OnServerEvent:Connect(function(player, bulletOrigin, mouse)
	local character = player.Character
	local bullet = Ray.new(bulletOrigin, (mouse - bulletOrigin))
	local hit, position = workspace:FindPartOnRayWithIgnoreList(bullet, {character.Head, character.Torso}, false, false)
	print(position)
	if hit then
		print(hit.Name)
	end
end)

cilentside:

local playerService = game:GetService("Players")
local camera = workspace.CurrentCamera
local localPlayer = playerService.LocalPlayer
local mouse = localPlayer:GetMouse()

mouse.Button1Down:Connect(function()
	game.ReplicatedStorage.OnShootRemote:FireServer(camera.Gun.BulletOrigin.CFrame.p, mouse.Hit.p)
end)

video demonstration: https://youtu.be/-X5QWlhzYec

1 answer

✓ Accepted answer

This happens because most of the time the ray doesn't intersect the part, just touches it barely. Rays detect intersections only, so you should extend the length of the ray so that it intersects the part, like this:

local bullet = Ray.new(bulletOrigin, (mouse - bulletOrigin).unit * 100)

This will ensure that the length of the ray is always 100 studs.

This discussion is preserved as it was. New answers, comments, and votes are closed.