Strange inaccuracy in raycasting

I wanted to add a gun to my game but I don’t know enough about raycasting to make a good one and the part that visualizes the ray is going all over the place

here is the script:

local mouse = player:GetMouse()
local camera = workspace.CurrentCamera
local Mouse3dRay: Ray = camera:ViewportPointToRay(mouse.X, mouse.Y, 1)
local range = script.Parent.Range.Value + player.Range.Value -- the player has a range value in the game I'm making
local ray: RaycastResult = workspace:Raycast(Mouse3dRay.Origin, Mouse3dRay.Direction*range)
local Origin = script.Parent.Handle.Position
local Direction =  Mouse3dRay.Direction*range	
local part = Instance.new("Part")
local len = (Origin - (Origin + Direction)).Magnitude
part.Size = Vector3.new(RAY_RADIUS, RAY_RADIUS , len)
part.Material = Enum.Material.SmoothPlastic
part.BrickColor = RAY_COLOR
part.Transparency = RAY_TRANSPARENCY
part.CFrame = CFrame.new(Origin, Direction) * CFrame.new(0, 0, -len*0.5)
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
if ray.Instance then
	if ray.Instance.Parent:FindFirstChild("Humanoid") then
		local target = ray.Instance
		if target.Parent:FindFirstChild("Enemy") then
			game.ReplicatedStorage.ClassesStuff.ShotEvent:FireServer(num,target)
		end
	end
end

this is the part of the script where I think the problem is coming from. And I only made some of this code the rest of it are little bits and pieces of code I found.

here is a image of the inaccuracy:


so you might notice that the part is going way below the baseplate.

I do not know why this is happening and there are no errors unless I click the sky, then the error is

Workspace.42Spider_man42.RangedGuyWeapon.ShootScript:59: attempt to index nil with 'Instance'

Yeah I don’t really care about that error I would just like to have to raycast accurate and the part where the ray is, thanks!

So I edited the script a tiny bit and now the parts go to the very center of whatever you click on
which is an improvement except now there’s two errors
but first here’s the altered script:

local mouse = player:GetMouse()
		local camera = workspace.CurrentCamera
		local Mouse3dRay: Ray = camera:ViewportPointToRay(mouse.X, mouse.Y, 1)
		local range = script.Parent.Range.Value + player.Range.Value
		local ray: RaycastResult = workspace:Raycast(Mouse3dRay.Origin, Mouse3dRay.Direction*range)

		local Origin = script.Parent.Handle.Position
		
		local Direction = mouse.Target.Position
		local part = Instance.new("Part")
		local len = (Origin - (Origin + Direction)).Magnitude
		part.Size = Vector3.new(RAY_RADIUS, RAY_RADIUS , len)
		part.Material = Enum.Material.SmoothPlastic
		part.BrickColor = RAY_COLOR
		part.Transparency = RAY_TRANSPARENCY
		part.CFrame = CFrame.new(Origin, Direction) * CFrame.new(0, 0, -len*0.5)
		part.Anchored = true
		part.CanCollide = false
		part.Parent = workspace
		if ray.Instance then
			if ray.Instance.Parent:FindFirstChild("Humanoid") then
				local target = ray.Instance
				if target.Parent:FindFirstChild("Enemy") then
					game.ReplicatedStorage.ClassesStuff.ShotEvent:FireServer(num,target)
					CanSwing = false

					script.Parent.Swinging.Value = true
					wait(script.Parent.SwingGaps.Value - player.AttackSpeed.Value/2)
					script.Parent.Swinging.Value = false
					CanSwing = true
				end
			end
		end

and here are the errors:

 Workspace.42Spider_man42.RangedGuyWeapon.ShootScript:46: attempt to index nil with 'Position'

that’s when I click the sky

Workspace.42Spider_man42.RangedGuyWeapon.ShootScript:58: attempt to index nil with 'Instance'

and that’s when something is out of range.

I am somewhat fine with where the parts go and the accuracy of the raycasts but the errors are probably not good. So if anyone knows the cause of the errors or knows how to get rid of them then that would be great!

If the ray does not intersect with anything, it won’t return anything.

In that scenario you could use the origin, and the given direction, capped to some maximum length.

1 Like

Do if ray and ray.Instance to fix this.

Also change this to mouse.Hit.Position, and see if it works.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.