So I am working on a gun system for a game and for now im just creating parts to represent the bullets however, I noticed occasionally the rays will go in a complete random direction
I’m also using a mix of my old code from a system from a few years ago so please excuse any mess. This is whats doing the actual like raycasting
local module = function(camera : CFrame, weapon, plr : Player)
local modules = game:GetService("ReplicatedStorage").WSAv3.Modules
local remotes = game:GetService("ReplicatedStorage").WSAv3.Remotes
local functions = require(modules.Functions)
local damageHumanoid = require(modules.DamagePlayer)
local config = game:GetService("ReplicatedStorage").WSAv3.WeaponConfigs:FindFirstChild(weapon.."-CONFIG")
if config then
local config = require(config)
local direction = camera.LookVector
local ray1 = Ray.new(camera.Position, direction*config.MaxDist)
local ignoreList = require(script.Parent.RayIgnoreList)
for _, child in plr.Character:GetDescendants() do
table.insert(ignoreList, child)
end
local result = workspace:FindPartOnRayWithIgnoreList(ray1, ignoreList, true, true)
print(result)
local intersection = result and result.Position or camera.Position + direction*config.MaxDist
local distance = (camera.Position - intersection).Magnitude
local rounded = math.floor(distance + 0.5)
local bullet = Instance.new("Part")
bullet.Size = Vector3.new(0.1, 0.1, distance)
bullet.CFrame = CFrame.new(camera.Position, intersection)*CFrame.new(0, 0, -distance/2)
bullet.Parent = workspace.MouseFilter
bullet.Anchored = true
bullet.CanCollide = false
bullet.BrickColor = BrickColor.Yellow()
--[[
]]
wait(2)
bullet:Destroy()
end
end
return module