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
end
-- 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)
end
This is just the essentials of the script basically.
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.
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.
Video:
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
end
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.