Raycast goes in weird directions (check solution)

I wanted to make a basic fps system, but the raycast that I want the gun to do when shooting is returning nothing when I use parameters, I’m assuming it stops at the excluded parts and ends there?

If I don’t use parameters it would just hit a part of my gun, so should I turn off canquery and cantouch on the gun or is there an issue in my script?

Update: I found out that the problem was I forgot to multiply the lookvector of the camera, but now the issue is that the raycast only detects a single part

Here is the RaycastParams:

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {tool}

And the code that makes the gun shoot (it’s unfinished so it will only play an animation for now):

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local animator = humanoid:FindFirstChild("Animator")
local cam = game.Workspace.CurrentCamera
local tool = script.Parent
local equipanim = tool.Animations.EquipAK74
local idleanim = tool.Animations.IdleAK74
local walkanim
local inspectanim = tool.Animations.InspectAK74
local fireanim = tool.Animations.FireAK74
local loadequipanim = animator:LoadAnimation(equipanim)
local loadidleanim = animator:LoadAnimation(idleanim)
local loadwalkanim
local loadinspectanim = animator:LoadAnimation(inspectanim)
local loadfireanim = animator:LoadAnimation(fireanim)

UIS.InputBegan:Connect(function(input, gameProcessedEvent)

	if input.UserInputType == Enum.UserInputType.MouseButton2 and tool.Parent == char then
		aiming = true
	elseif input.UserInputType == Enum.UserInputType.MouseButton1 and canshoot == true and tool.Parent == char then
		currentlyshooting = true
		caninspect = false
		print("shot")
		repeat
			local function recoil()
				if canshoot == true and tool.Parent == char then
					canshoot = false
					loadfireanim:Play()
					local castray = game.Workspace:Raycast(tool.Barrel1.Position, cam.CFrame.LookVector, params)
					if castray and castray.Instance then
						print(castray.Instance)
					end
				end
			end
			recoil()
			task.wait(RPM)
			canshoot = true
		until currentlyshooting == false or automatic == false or tool.Parent ~= char

	end
	

end)

if you exclude the tool itself, it doesn’t have an origin. Instead of excluding the tool, exclude the character. so it doesn’t do damage to your character. maybe you can also exclude the weapon mesh aswell, but keep the handle.

Shouldn’t the gun be shooting at what your mouse is pointing at? Anyways though, I think you should add these two variable:

local mouse = game.UserInputService:GetMouseLocation()
local ray = cam:ViewportPointToRay(mouse.X, mouse.Y)

And replace cam.CFrame.LookVector to ray.Direction instead. I’m not sure if this will work though, so you can reply me saying if it works or not after you try it.

Update: I found out that the problem was I forgot to multiply the lookvector of the camera, but now the issue is that the raycast only detects a single part

1 Like

Raycasts work with a direction vector apparently (idk how that works but I’ve tried with the mouse position before and it gave me weird results) I fixed the returning nothing part by multplying the cam.CFrame.LookVector because I forgot about it, but I still have the problem that it gives the wrong part, imma try to vizualise it with a beam or something like that to see where the ray goes.

After trying with the mouse’s LookVector, it somewhat works but it goes in weird directions.

local castray = game.Workspace:Raycast(tool.Barrel1.Position, player:GetMouse().Hit.LookVector * range, params)
1 Like

It works fine even with the tool excluded, the problem was I forgot to multiply the LookVector of the camera’s CFrame.

But now it goes in weird directions, I’ll see if I can fix that

So I finally found out the problem, I was shooting the ray from the gun’s barrel and not the camera. Apparently that made it go in weird directions

Marking this as solution and changing the topic name in case others have the same problem

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