Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Raycast gun shooting in random directions?

Asked by 3 years ago

I have a raycast gun which I programmed which mostly works, however at times (usually when the large cylinders spawn) the rays veer off into nowehere, not following my mouse, like so:

https://gyazo.com/4f65f58bdfb96e0ef6c39ab0c65d8401

The gun uses a LocalScript, Script and RemoteEvent all in the same part (in the gun). The LocalScript fires the RemoteEvent whenever the mouse key is held down. Here is the code in the LocalScript:

local fire = function(player, mouseLocation)
  if player.character ~= tool.Parent then return end
  
  -- find where the mouse is pointing
  local rayOrigin = script.Parent.Position
  local rayDirection = (mouseLocation.p - script.Parent.Position).Unit * 300
  
  -- cast the ray
  raycastParams.FilterDescendantsInstances = {player.character, game.Workspace.corporeal}
  local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
  
  -- handle results [Omitted since I think it's irrelevant]
  
  -- create "gunfire"
  local shotPosition = hitPos or rayDirection
  local shot = Instance.new("Part")
  shot.Color = Color3.new(1, 1, 0)
  shot.CFrame = CFrame.new(script.Parent.Position:Lerp(shotPosition, 0.5), shotPosition)
  shot.Anchored = true
  shot.Size = Vector3.new(0.1, 0.1, (script.Parent.Position - shotPosition).magnitude)
  shot.CanCollide = false    
  shot:SetAttribute("shotPosition", shotPosition)
  local bmscript = script.bulletMovement:Clone()
  bmscript.Disabled = false
  bmscript.Parent = shot
  
  shot.Parent = script.Parent
  Debris:AddItem(shot, 0.05)
  

end

script.Parent.shoot_input.OnServerEvent:Connect(fire)

"corporeal" is a folder in the workspace that contains parts that I want the gun to ignore (the cylinders and balls and stuff are in there)

I have confirmed that the LocalScript always accurately gets the mouse position and passes it; I think the problem is with the raycasting but IDK what it is exactly. Any help would be greatly appreciated

1 answer

Log in to vote
0
Answered by 3 years ago

adding mouse.TargetFilter fixed the issue.

Ad

Answer this question