Why are raycasts so broken? Are there any working alternatives?

Raycasts are an engineering disaster. They’re broken and make absolutely no sense. On several occasions, I’ve spent hours trying to figure out raycasts, all to no avail. I haven’t struggled this much with anything else.

I’ve watched many tutorials, studied the inner workings of many free models, and tried my hand at doing it on my own, each several times. I even asked my friends for help, and they’re just as stumped as I am. I just cannot figure out how to tame this beast of game engineering.

Sometimes the origin is inexplicably 0,0,0, it returns nil even when the barrel is point blank and down an enemy’s throat, it takes up an unhealthy amount of lines in my script, it’s an absolute maze to work with, and it just doesn’t work with the mouse.

Can someone tell me why raycasts are such a terror? Has anyone cracked the code and figured out how raycasts work? Are there any alternatives for raycasts?

Please respond quickly. I seriously need help.

1 Like

I would not recommend this, but you could use UserInputService and get MouseButton1 and get mouse.Target or mouse.Hit

I’ve made guns before and they’ve worked just fine. The bullets pointed towards the cursor, they had working spread, they could kill enemies, but the guns in question could shoot through walls.

I’ve done this before, and it works, but I’m not satisfied with it.

Could you maybe show some of your code so we can see what the problem is?

I’ve kind of trashed some of it because I gave up, however I can show you what I have right now.

function raycast(startpos,vec)
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = tool.Parent:GetChildren()
	local raycast = workspace:Raycast(startpos,vec,params)
	if raycast ~= nil then
		local hit = raycast.Instance
		local pos = raycast.Position
		print(hit)
		print(pos)
		return hit, pos
	else
		print("Raycast is nil")
	end
end

---------------

function shoot(position)
	for i = 1, shots do
		delay(0,function()
			local stupid = tool.Barrelpos.CFrame.LookVector
			
			local hit, endpos = raycast(tool.Barrelpos.Position,stupid)
		end)
	end
end

And what is the problem here? Sorry for making you talk so much.

It always returns nil. No matter what. Absolutely no fixes for this, I’ve had tons of other issues with raycasts in the past. I just need a good alternative that stops bullets from going through walls.

Using projectiles is archaic and buggy so I’m not going to use that.

A working raycast function I could use would totally work as well, if you have any. Raycasts are a convoluted mess, so don’t fret if you don’t have any.

The problem might be is because when using the variable vec you are never multiplying it by anything so it never goes anywhere, multiply it by the amount of studs you want the Raycast to travel

I tried multiplying it and that constantly hit the baseplate or returned nil, but as of now I have it set to the barrel part’s LookVector.

local stupid = tool.Barrelpos.CFrame.LookVector
local stupid = tool.Barrelpos.CFrame.LookVector * number

try this

I think it actually works! I just need to find a way to make the gun point up and down now.
Thanks for your help.
image

1 Like

No problem, happy coding.

char lol

Actually, I don’t even have to make it point up and down since I just made it work with the mouse!
Today is a good day.

local stupid = CFrame.new(tool.Barrelpos.Position, position).LookVector * range
1 Like

That’s great! Good game lmao. sdfklsdf