Hello!
I made a weapon that shoots bullets when triggered by the mouse. The main problem I’m having is bullet detection. I tried a lot of things and some worked but it was really bad at detecting the CharacterModel parts. The thing is that the gun has spread so I need it as accurate as possible when it finds something. Im using raycast to make it.
I’m currently using raycasting but all my methods are working terrible sometimes it works and sometimes it just doesn’t print any detections. So I’m trying to get a specific method to do it.
local LookRay = Ray.new(Bullet.Position, Bullet.CFrame.lookVector + Vector3.new(0.2,0.2,0.2))
local LookRay2 = Ray.new(Bullet.Position, -Bullet.CFrame.lookVector + Vector3.new(0.2,0.2,0.2))
local SideRay = Ray.new(Bullet.Position, Bullet.CFrame.RightVector + Vector3.new(0.2,0.2,0.2))
local SidekRay2 = Ray.new(Bullet.Position, -Bullet.CFrame.RightVector + Vector3.new(0.2,0.2,0.2))
local UpRay = Ray.new(Bullet.Position, Bullet.CFrame.UpVector + Vector3.new(0.2,0.2,0.2))
local UpRay2 = Ray.new(Bullet.Position, -Bullet.CFrame.UpVector + Vector3.new(0.2,0.2,0.2))
local Rays = {
LookRay;
LookRay2;
SideRay;
SidekRay2;
UpRay;
UpRay2;
}
while true do
wait(0.001)
for i,SelectedRay in pairs(Rays) do
local hit, pos = workspace:FindPartOnRay(SelectedRay)
if hit then
print("Found not you lol")
Bullet:Destroy()
print(hit.Parent.Name)
if hit.Parent:FindFirstChildOfClass("Humanoid") and Landed == false then
Landed = true
hit.Parent:FindFirstChildOfClass("Humanoid").Health = hit.Parent:FindFirstChildOfClass("Humanoid").Health - SettingsModule.Damage
print("Bullet gone")
break
end
end
end
end
Another way I tried was by doing them one by one with these
local HF, PF = workspace:FindPartOnRay(FrontRay);
local HB, PB = workspace:FindPartOnRay(BackRay);
local HR, PR = workspace:FindPartOnRay(RightRay);
local HL, PL = workspace:FindPartOnRay(LeftRay);
local HU, PU = workspace:FindPartOnRay(UpRay);
local HD, PD = workspace:FindPartOnRay(DownRay);
Yes it’s an automatic weapon so when I hold down my mouse it shoots where the mouse is with little spread. I parent the bullets into a folder named “Debris” inside of workspace
one problem i see is
Ray.new(Bullet.Position, -Bullet.CFrame.UpVector + Vector3.new(0.2,0.2,0.2))
you specified a origin and a direction, but your direction is literally only 0.2 studs long, multiply it with the amount of studs you want your ray to travel