I am attempting to make a gun system, but I honestly have no clue what’s wrong with my raycasting. It’s difficult to explain with words, but the rays almost start to return to the origin of the ray, which would be the HumanoidRootPart. I’m using the HumanoidRootPart instead of the barrel of the gun for the origin because I was having problems shooting through walls. This fixed that issue, but it created the issue in question. Does anyone know why this is happening, and how I can avoid it next time? I’m not too familiar with raycasting, so I apologize if this is a simple fix.
function Fire_Auto()
while Mouse1Down == true and Reloading == false and Settings.CurrentAmmo > 0 and FireDebounce == false and Aiming == true do
FireDebounce = true
Fire_Anim:Play()
Character.CameraSystemLocal.ChangeCamera:Fire("Fire", Settings.Recoil)
Settings.CurrentAmmo = Settings.CurrentAmmo - 1
local ray = Ray.new(Player.Character.HumanoidRootPart.Position, (Mouse.Hit.p - Player.Character.HumanoidRootPart.Position).unit * Settings.MaxBulletDistance)
local part, position = workspace:FindPartOnRay(ray, Player.Character, false, true)
Fire_RE:FireServer(position)
local Beam = Instance.new("Part", workspace)
Beam.FormFactor = "Custom"
Beam.Transparency = 0.6
Beam.Material = "Neon"
Beam.Color = Color3.new(1, 0, 0)
Beam.Anchored = true
Beam.Locked = true
Beam.CanCollide = false
Beam.Name = "Bullet"
local Distance = (script.Parent.Handle.Muzzle.WorldPosition - position).magnitude
Beam.Size = Vector3.new(0.1, 0.1, Distance)
Beam.CFrame = CFrame.new(script.Parent.Handle.Muzzle.WorldPosition, position) * CFrame.new(0, 0, -Distance / 2)
game:GetService("Debris"):AddItem(Beam, 0.05)
if part then
local DetectedHumanoid = part.Parent:FindFirstChild("Humanoid")
if not DetectedHumanoid then
DetectedHumanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if DetectedHumanoid then
InflictTarget_BE:FireServer(DetectedHumanoid, part, position)
elseif not DetectedHumanoid then
end
end
wait(Settings.FireRate)
FireDebounce = false
end
end