Trying to make it so that my raycast ignores all can collidable parts.
I made it ignore it in the raycast, however it for some reason like because the mouse still sees the collidable part, it disorts it and gives me this weird issue. I want to figure out how to raycast to where the mouse is clicked and ignore certain parts completely.
function Framework:Raycast(Origin, EndPos, IgnoreList, Range, CallCheck, MaxRunTimes, RunTimes)
local Params = RaycastParams.new();
local Distance = (EndPos - Origin).unit;
local RayResults
Params.FilterType = Enum.RaycastFilterType.Exclude;
Params.IgnoreWater = true;
Params.FilterDescendantsInstances = IgnoreList; -- includes the collideable part
RayResults = workspace:Raycast(Origin, Distance * Range, Params);
return RayResults
end
-- how its called
local RayResults = self:Raycast(self.Tool.Main.CFrame.p, Vector3.new(mousePos.x + (math.random() * (spread * 2) - spread), mousePos.y + (math.random() * (spread * 2) - spread), mousePos.z + (math.random() * (spread * 2) - spread)), self:GetIgnoreList(game.Players.LocalPlayer.Character), self.Config.Range and self.Config.Range or 500, function(RayResults)
if RayResults.Instance:IsDescendantOf(workspace.Assets.Interactables.Vehicles) then return false end
if RayResults.Instance.CanCollide == false or RayResults.Instance.Transparency == 1 then
return true
end
if RayResults.Instance:IsDescendantOf(workspace.CurrentCamera) then
return true
end
return false
end, self.Config.MaxRetries and self.Config.MaxRetries or 10)
Basically i need a different way to get mouse hit or have the mouse ignore a part for the raycast
its because of how you get mousePos, im guessing right now you’re using mouse.Hit.p, which is probably causing this issue
i would use camera:ViewportPointToRay() with the mouse’s position, and then from the ray you can get it’s Direction
I am assuming you’re using mouse.Hit.p, which is most likely where the issue originates. I had a script that operated similar to yours, and my solution worked out well.
Try using a raycast via camera:ViewportPointToRay.
So I tried that and I got a issue of it seeming off from where im clicking; heres the code
local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local ignoreParts = {}
local function castLaser()
local handle = tool:FindFirstChild("Handle")
if not handle then return end
local rayOrigin = handle.Position
local ray = workspace.CurrentCamera:ViewportPointToRay(mouse.X, mouse.Y)
local rayDirection = ray.Direction * 1000
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
rayParams.FilterDescendantsInstances = ignoreParts
rayParams.IgnoreWater = true
local result = workspace:Raycast(rayOrigin, rayDirection, rayParams)
local laser = Instance.new("Part")
laser.Anchored = true
laser.CanCollide = false
laser.Material = Enum.Material.Neon
laser.BrickColor = BrickColor.new("Bright red")
laser.Size = Vector3.new(0.2, 0.2, (result and (rayOrigin - result.Position).Magnitude) or 1000)
laser.CFrame = CFrame.new(rayOrigin, rayOrigin + rayDirection) * CFrame.new(0, 0, -laser.Size.Z / 2)
laser.Parent = workspace
game:GetService("Debris"):AddItem(laser, 0.1)
return result
end
tool.Activated:Connect(function()
castLaser()
end)
for i,v in pairs(workspace:GetDescendants()) do
if v:IsA("BasePart") and v.CanCollide == false then
table.insert(ignoreParts, v)
end
end
is it off only vertically, like is it shooting above your mouse or below it? if it is, then try using ScreenPointToRay instead of ViewportPointToRay because it accounts for the inset that Mouse.X/Mouse.Y have