Ray casting not working properly

https://gyazo.com/c5566420c90fe3066f9fe8c888fb864f
As you can see, there’s a weapon (clearly lol)
So basically, I’m ray casting it, but for some reason, sometimes it works, sometimes no, I don’t exactly know why…
Here you can see code

NOTE:

I think it might be for the arm script, I don’t know…

This is how gun look:
image

local function Shot()
local Distance = (Mouse.Hit.Position - Tool.Body.FirePart.Position) 
	local ray = Ray.new(Tool.Body.FirePart.CFrame.Position, (Mouse.Hit.Position - Tool.Body.FirePart.Position).Unit * Distance.Magnitude)
	local hit,position = workspace:FindPartOnRayWithIgnoreList(ray, {Tool.Handle,Tool.Body,Character, Camera})
	if hit then
     Tool.Events.Hit:FireServer(position,hit)	
	end
end

And serverside:

Tool.Events.Hit.OnServerEvent:Connect(function(Player, position,hit)
	if hit.Parent:FindFirstChild('Humanoid') then
		if hit.Name == 'Head' then
			hit.Parent.Humanoid:TakeDamage(100)
			elseif hit.Name ~=  'Head' then
				hit.Parent.Humanoid:TakeDamage(20)
		end
	end
	if not hit.Parent:FindFirstChild('Humanoid') then
   local Part = Instance.new('Part', workspace)
Part.Size = Vector3.new(0.2,0.2,0.2)
Part.Position = position + Vector3.new(0,Part.Size.Y/2,0)
Part.CFrame = CFrame.new(Part.Position)
Part.Anchored = true
Part.CanCollide = false
Part.BrickColor = BrickColor.new('Black')
Part.Transparency = 1
local Particle = Instance.new('ParticleEmitter', Part)
Particle.Texture = 'rbxasset://textures/particles/smoke_main.dds'
Particle.Size = NumberSequence.new(0.5)
Particle.LightInfluence = 1
Particle.LightEmission = 0
Particle.Acceleration = Vector3.new(0,0,0)
Particle.Lifetime = NumberRange.new(0.4)
Particle.Rate = 12
Particle.Rotation = NumberRange.new(0)
Particle.Speed = NumberRange.new(2)
Particle.SpreadAngle = Vector2.new(0,0)
game:GetService('Debris'):AddItem(Part, 0.5)
game:GetService('Debris'):AddItem(Part, 0.5)
end)

example:
image
It detected hit 13 times (while aiming straight to baseplate)
of 18 shots.

It may be because you are only firing the Ray once. I use raycasting an usually I’d fire it a few times just to make sure it actually goes. Since you’re creating a gun I recommend doing it twice at least.

1 Like

Maybe the ray is just missing the baseplate. Have you tried changing Distance.Magnitude to something like 1000000 so you extend the ray further in that direction?

2 Likes

I think the max lenght is like 5000 so I’ll change to that
EDIT: It worked?

Yup, I’m guessing there was some kind of floating point error causing it to only register the basepart part of the time.

But, how would I do it like, my way? Like
Checking the distance

Is there a reason not to have your ray extend further in that direction? If you need to limit the distance you could check the distance of the cast afterwards using the hit position and limit based on that.

Thinking about it, maybe you could just multiply by the distance plus some small epsilon and that would be close enough.

e.g. local ray = Ray.new(Tool.Body.FirePart.CFrame.Position, (Mouse.Hit.Position - Tool.Body.FirePart.Position).Unit * (Distance.Magnitude+0.0001))

1 Like