Issue with raycast, going through objects/ not detecting them

Im raycasting from the characters HumanoidRootPart, adding the height of the head, and towards the mouse hit position. For some reason, the raycast works on some walls, sometimes. And sometimes it doesnt work at all on some walls. Theyre all the same walls, in the same collision group. Sometimes it works on the ground, sometimes it goes through the ground. Some times when it goes through the ground, it ends up in the void at like + 32839123192381290 or something. I’m lost on what’s causing the issue.

Here’s the code ;

function global.itemFunctions:makeRay2(Filter,Instances,Origin,Direction)
	local RayP_Main = RaycastParams.new()
	RayP_Main.FilterType = Filter
	RayP_Main.CollisionGroup = "Default"
	RayP_Main.FilterDescendantsInstances = Instances
	RayP_Main.IgnoreWater = true

	return workspace:Raycast(Origin, Direction, RayP_Main)
end

local CrosshairRay1 = G.itemFunctions:makeRay2(Enum.RaycastFilterType.Exclude,
		{G.Character,G.Camera},
		(G.Character.PrimaryPart.CFrame * CFrame.new(0,(G.Character.PrimaryPart.Size.Y/2 + G.Character.Head.Size.Y/2),0)).Position, 
		G.Mouse.Hit.p - (G.Character.PrimaryPart.CFrame * CFrame.new(0,(G.Character.PrimaryPart.Size.Y/2 + G.Character.Head.Size.Y/2),0)).Position)

if CrosshairRay1 then 
	if tracer1 then tracer1:Destroy() end
	local start = G.Character.PrimaryPart.CFrame * CFrame.new(0,(G.Character.PrimaryPart.Size.Y/2 + G.Character.Head.Size.Y/2),0).Position
	local dist = (start-CrosshairRay1.Position).Magnitude
	tracer1 = Instance.new("Part")
	tracer1.Name = "Tracer"
	tracer1.Anchored = true
	tracer1.CanCollide = false
	tracer1.Size = Vector3.new(0.2, 0.2, dist)
	tracer1.CFrame = CFrame.new(start,CrosshairRay1.Position)*CFrame.new(0, 0, -dist/2)
	tracer1.Parent = G.Character
	tracer1.BrickColor = BrickColor.new("Bright red")
end

Example of it happening

Example of it working randomly on a wall, and then cutting out right next to the same spot.

1 Like

I built it into a local script for starter character, outside my place
It is still not getting a result half the time… even when not moving the mouse whatsoever it will get results only some times.

wait(1)


local G = {
	Character = script.Parent,
	Mouse = game.Players.LocalPlayer:GetMouse(),
	Camera = game.Workspace.CurrentCamera,
}
function makeRay2(Filter,Instances,Origin,Direction)
	local RayP_Main = RaycastParams.new()
	RayP_Main.FilterType = Filter
	RayP_Main.CollisionGroup = "Default"
	RayP_Main.FilterDescendantsInstances = Instances
	RayP_Main.IgnoreWater = true

	return workspace:Raycast(Origin, Direction, RayP_Main)
end

local tracer1 = nil

game:GetService("RunService").RenderStepped:Connect(function()
	
	local CrosshairRay1 = makeRay2(Enum.RaycastFilterType.Exclude,
		{G.Character,G.Camera,tracer1},
		(G.Character.PrimaryPart.CFrame * CFrame.new(0,(G.Character.PrimaryPart.Size.Y/2 + G.Character.Head.Size.Y/2),0)).Position, 
		G.Mouse.Hit.p - (G.Character.PrimaryPart.CFrame * CFrame.new(0,(G.Character.PrimaryPart.Size.Y/2 + G.Character.Head.Size.Y/2),0)).Position)
	
	if CrosshairRay1 then 
		print' exists.'
		if tracer1 then tracer1:Destroy() end
		
		local start = G.Character.PrimaryPart.CFrame * CFrame.new(0,(G.Character.PrimaryPart.Size.Y/2 + G.Character.Head.Size.Y/2),0).Position
		local dist = (start-CrosshairRay1.Position).Magnitude
		tracer1 = Instance.new("Part")
		tracer1.Name = "Tracer"
		tracer1.Anchored = true
		tracer1.CanCollide = false
		tracer1.Size = Vector3.new(0.2, 0.2, dist)
		tracer1.CFrame = CFrame.new(start,CrosshairRay1.Position)*CFrame.new(0, 0, -dist/2)
		tracer1.Parent = G.Character
		tracer1.BrickColor = BrickColor.new("Bright red")
	else
		print 'doesnt exist.???'
	end
end)

Example again

It ended up being a distance issue, just multiplied it by a large number.

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