Raycast ending up in unexpected positions

So after some cruising around the DevForum, I couldn’t find the answer to my problem so I’m making a post for it instead:

So I’m working on a gun and I want to create a bullet hole at a raycast’s end position. That’s fine for the most part, no issues there.

Until I end up with a stray ray. When I keep firing the gun, eventually I end up with a single bullethole that is in some weird direction that is nearby but not exactly at the mouse’s position.

I was thinking that it was maybe because of the OTS camera I use right now, but when I turned off lockcenter and did some other bits of testing, the stray bullet still appears.

Here’s what I use:

local RaycastParameters = RaycastParams.new() 
RaycastParameters.IgnoreWater = true
RaycastParameters.FilterType = Enum.RaycastFilterType.Blacklist

function Raycast(StartPosition, EndPosition, Settings, Spread)
	RaycastParameters.FilterDescendantsInstances = {Player.Character, workspace.RaycastIgnoreList
	local SpreadIns = Vector3.new(0,0,0) -- disabled for now

	local Raycast = workspace:Raycast(StartPosition, ((EndPosition + SpreadIns - StartPosition)).Unit * Settings.MaxDistance, RaycastParameters)
	return Raycast

-- Inside a function:

local RaycastResult = Raycast(BarrelPoint.Position, Mouse.Hit.Position, Settings, CurrentSpread)

if RaycastResult then
	local Hole = GunAssets.Regular:Clone()
	Hole.Parent = workspace.RaycastIgnoreList
	Hole.CFrame = CFrame.new(RayPos, RayPos + RayNor)
	DebrisService:AddItem(Hole, 6)

This is just the essentials of the script basically.


Position data w/ inconsistencies:

1 Like

Most likely its the mouse.Hit raycast being interfered by the bullet hole. To fix this, do the raycast yourself using your own ignore list. Since the bullet only lands in the center of your screen, you can use the camera’s lookvector as the direction. e.g. local ray = workspace:Raycast(camera.CFrame.Position, camera.CFrame.LookVector * 999, raycastParameters)
and use that ray’s position as the replacement for mouse.hit.position.


Maybe try this? I tried to fix your spread as well.

Works alright, but then I have the problem of people being able to shoot around corners, since if I make the starting position the barrel, it ends up offset.

Doesn’t follow the mouse.

Try first raycasting to the mouse location to get a target location in 3d. Then raycast for the projectile from the gun to the target location.

Works fine, but it still has the same issue as before (that one unexpected bullet)

I do wish to note that I do filter out the bullet holes from Mouse.Hit using:
Mouse.TargetFilter = workspace.RaycastIgnoreList
right before I call the function. All the bullet holes are placed under this list.

function Raycast(StartPosition, HitCFrame, Settings, Spread) -- HitCFrame is Mouse.Hit
	RaycastParameters.FilterDescendantsInstances = {Player.Character, workspace.RaycastIgnoreList}
	local SpreadIns = Vector3.new(0,0,0) --Still disabled for now.
	local Direction = HitCFrame.LookVector + HitCFrame:VectorToWorldSpace(SpreadIns) + (HitCFrame.Position - StartPosition)

	local Raycast = workspace:Raycast(StartPosition, Direction * Settings.MaxDistance, RaycastParameters)
	return Raycast

Try printing the name of the hit part with the outlier information, to help give more insight and be sure.

1 Like


1 Like

How large is Settings.MaxDistance?

1 Like

Currently it is 500. (Char limit)

I would suggest setting Direction to just HitCFrame.Position-StartPosition and see if you still see the issue.

Still the same issue. It just returns us to what I had initially:
(EndPosition + SpreadIns - StartPosition).Unit * Settings.MaxDistance

Just without the .Unit and SpreadIns.

Dont replace the raycast from the muzzle completely, use both. Shoot the camera lookvector ray first then shoot a ray from the muzzle to the camera’s ray position and use that as the final bullet position.

1 Like

Ah I see. Works great (after testing like 4 times I haven’t experienced the stray bullet), I’ll mark your original post as the solution.

Thanks, have a great day!