What is the best way to create bullet detection?

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.

A script example would be nice thanks!

2 Likes

Use a .Touched function so the part detects the bullet when it touches it.

1 Like

I tried .Touched but whenever the model wasn’t moving it wouldn’t detect well…

raycasting, touched function for bullets is horrible

1 Like

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.

raycasting is very accurate, could it be because you are not casting the ray properly?

Probably so should I send an example?

yes that would be very helpful

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

This is one of my older ways I did it

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);

this is interesting, are you firing rays when you click a mousebutton? and where are the bullets located, is there a gun?

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

ray.new(origin,direction)

you set the direction to only 0.2 studs , which is a very short number, is this intentional?

I sent it to add with the Vectors of the bullet which I guess yeah is only 0.2 studs so yes it was intentional but I couldn’t figure out a good size

Also here is a video of it

when you aim at the dummy from that distance, does it actually hit?

Theres usually a little gap between the dummy and the bullet sometimes it does damage and sometimes not

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

Ray.new(Bullet.Position, (-Bullet.CFrame.UpVector + Vector3.new(0.2,0.2,0.2))*RANGE)

Interesting!
Ill try it give me a minute to rewrite the Cast

also the most important thing i just noticed, you are launching the bullets at the enemy and then casting a ray to see if it touched right?

Yes it’s launching to my mouse and checking simultaneously