Raycast not working properly when shooting

The video below will explain what I’m experiencing. (I’m bad at explaining, ask my teacher if you want)

I’ve rewritten this function at least five times now and have gotten the same results. The current version is from Roblox Battle Royale’s (lol) weapons.

local function SpawnRayCast(StartPos, Direction, Length)
local Hit, EndPos = workspace:FindPartOnRayWithIgnoreList(Ray.new(StartPos,Direction * Length), {Camera, Tool.Parent})
if Hit then
	local FirePointObject = VMHandleToFire:FindFirstChild("GunFirePoint")
    if FirePointObject ~= nil then
    	local tipCFrame = FirePointObject.WorldCFrame
    	local tipPos = tipCFrame.Position
       	local tipDir = tipCFrame.LookVector
    	local amountToCheatBack = math.abs((Camera.Arms.Handle.Position - tipPos):Dot(tipDir)) + 1
    	local gunRay = Ray.new(tipPos - tipDir.Unit * amountToCheatBack, tipDir.Unit * amountToCheatBack)
		local hitPart, hitPoint = Roblox.penetrateCast(gunRay, {Camera, Tool.Parent})
		if hitPart and math.abs((tipPos - hitPoint).Magnitude) > 0 then
			return SpawnRayCast(EndPos + Direction * 0.1, Direction, Length - ((StartPos - EndPos).magnitude))
        end
    end	
end
   return EndPos
end

This is how the function is used in the scripts.

SpawnRayCast(Camera.CFrame.p, (EndPos - Camera.CFrame.p).unit, 5000)
2 Likes

I think it would be important to show us what does the EndPos variable is about. I know it’s mouse location but I wanna make sure. Also, you should use workspace:Raycast and RaycastParamsnew() since I believe the other functions of raycastibg is deprecated.

1 Like

EndPos is the Crosshair’s AbsolutePosition converted into a ScreenPointToRay.

local InputRay = Camera:ScreenPointToRay(CurrentPosOnScreen.X, CurrentPosOnScreen.Y)
local EndPos = InputRay.Origin + InputRay.Direction
return SpawnRayCast(Camera.CFrame.p, (EndPos - Camera.CFrame.p).unit, 5000)
1 Like

Can I ask why don’t you just use the Mouse.hit.p?

My argument is… good point :moyai:. I’ll try that right now.

1 Like

Just tested it and got the same result.

Sorry for the late reply, can I see the new code?

I changed the both of the Camera.CFrame.p’s to Mouse.hit.p but both ended up just going straight up into the air.

local InputRay = Camera:ScreenPointToRay(CurrentPosOnScreen.X, CurrentPosOnScreen.Y)
local EndPos = Mouse.hit.p
return SpawnRayCast(Camera.CFrame.p, (EndPos - Camera.CFrame.p).unit, 5000)

I think the issue here might be with your use of the Camera:ScreenPointToRay() function, being that it doesn’t account for GuiInset. Had the same problem with my mouse-to-world function. You could try making a debug part and CFraming it to the ray’s raycast result (if you turn it into an actual raycast using workspace:Raycast), which would let you know how the variable is different from expected (if at all).

(sorry for late response) I’ve already tried using debugging part to see if anything was wrong but everything seems to be working fine. Forgot to mention it only does this when shooting through a window and very tight spaces.

It could have something to do with how your windows were designed. Are the window frames one big union? That could be the cause for the raycast stopping at the window.

The window is a mesh part but even when I remove the window the results are still the same.