Raycasting is not performing the intended way

I want to make a gun, but I have to use raycasting to do that. (I want to make my own because I want to make an AI for it.)

Most of the time raycast returns nil, sometimes it raycasts to random objects that I didn’t expect for it to raycast to.

I found a way to show the raycasts, but it is still as confusing.

Server


script.Parent.Equipped:Connect(function()
	script.Parent.Mesh.CanCollide = false
end)

local flare = script.Parent.Flare
script.Parent.Shoot.OnServerEvent:Connect(function(plr, target)
	local raycpr = RaycastParams.new()
	raycpr.FilterType = Enum.RaycastFilterType.Blacklist
	raycpr.FilterDescendantsInstances = {script.Parent}
	local rayc = game.Workspace:Raycast(flare.Position, (flare.Position - target.Position).Unit, raycpr)
	if rayc == nil then
		print("Raycast is nil! Must have done something wrong..")
	else
		print(rayc.Instance:GetFullName())
		local distance = (flare.Position - rayc.Position).Magnitude
		local p = Instance.new("Part")
		p.Parent = game.Workspace
		p.Anchored = true
		p.CanCollide = false
		p.Size = Vector3.new(0.1, 0.1, distance)
		p.CFrame = CFrame.lookAt(flare.Position, rayc.Position)*CFrame.new(0, 0, -distance/2)
	end
end)

Local

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
script.Parent.Activated:Connect(function()
	local target = mouse.Target
	script.Parent.Shoot:FireServer(target)
end)

Hiearchy
image

Model
image

Have you tried printing out the values of the variables passed to the raycast call?

instead of blacklisting script.Parent do script.Parent:GetChildren()

2 Likes


image

Add you character inside the blacklist table.

raycpr.FilterDescendantsInstances = {script.Parent, script.Parent.Parent}

Also try to expand the ray a little bit.

local rayc = game.Workspace:Raycast(flare.Position, (target.Position - flare.Position) * 1.1, raycpr)

It still returns nil, I tried adding :GetChildren() to both of them but it still didn’t work

Can you show the updated codes?

Server


script.Parent.Equipped:Connect(function()
	script.Parent.Mesh.CanCollide = false
end)

local flare = script.Parent.Flare
script.Parent.Shoot.OnServerEvent:Connect(function(plr, target)
	local raycpr = RaycastParams.new()
	raycpr.FilterType = Enum.RaycastFilterType.Blacklist
	raycpr.FilterDescendantsInstances = {script.Parent, script.Parent.Parent}
	local rayc = game.Workspace:Raycast(flare.Position, (flare.Position - target.Position) * 1.1, raycpr)
	if rayc == nil then
		print("Raycast is nil! Must have done something wrong..")
		print("Gun Position: "..tostring(flare.Position))
		print("Direction: "..tostring((flare.Position - target.Position).Unit))
		print("Raycast Params: "..tostring(raycpr))
	else
		print(rayc.Instance:GetFullName())
		local distance = (flare.Position - rayc.Position).Magnitude
		local p = Instance.new("Part")
		p.Parent = game.Workspace
		p.Anchored = true
		p.CanCollide = false
		p.Size = Vector3.new(0.1, 0.1, distance)
		p.CFrame = CFrame.lookAt(flare.Position, rayc.Position)*CFrame.new(0, 0, -distance/2)
	end
end)
(flare.Position - target.Position) * 1.1

change this one to

(target.Position - flare.Position) * 1.1

(Target Position - Origin Position) * 1.1

1 Like