Ray wont work as intended

I am making a gun and my gun uses rays to find the target and parts cause why not?
Currently this is what it does


you can see my mouse points in another direction and it point in another for some reason

heres the script

local Idle = script.Idle
local Tool = script.Parent
local Handle = script.Parent.Handle
local Anim:AnimationTrack

local UIS = game:GetService("UserInputService")
local Mouse = game.Players.LocalPlayer:GetMouse()

-- Values
local Ammo = script.Parent.Ammo
local MaxAmmo = script.Parent.MaxAmmo
local Ran = script.Parent.Range
local Dmg = script.Parent.Damage
local RelTime = script.Parent.ReloadTime

Tool.Equipped:Connect(function(m)
	Anim = Tool.Parent:WaitForChild("Humanoid").Animator:LoadAnimation(Idle)
	Anim:Play()
end)
Tool.Unequipped:Connect(function()
	Anim:Stop()
end)

local function Ammoo()
	return tonumber(Ammo.Value)
end

local function Reload()
	Ammo.Value = "Reloading..."
	wait(RelTime)
	Ammo.Value = MaxAmmo.Value
end

local event = game.ReplicatedStorage.Shot

local function point(pos,name)
	local point = Instance.new("Part",workspace)
	point.Anchored = true
	point.Size = Vector3.new(1,1,1)
	point.Shape = Enum.PartType.Ball
	point.Material = Enum.Material.Neon
	point.Position = pos
	point.CanCollide = false
	point.Name = name
end

Tool.Activated:Connect(function()
	if Ammoo() > 0 then
		Ammo.Value -= 1
		local wp = Handle.StartAt.WorldPosition
		local dir
		Handle.StartAt.Position += Vector3.new(0,0,Ran.Value)
		dir = Handle.StartAt.WorldPosition
		Handle.StartAt.Position -= Vector3.new(0,0,Ran.Value)
		local bulletray = Ray.new(wp,dir) -- making of ray
		local part,pos = workspace:FindPartOnRay(bulletray,Handle,true,false)
		point(pos,part.Name)
	else
		Reload()
	end
end)


--[[
UIS.InputBegan:Connect(function(i,gpe)
	if gpe then return end
	if i.KeyCode == Enum.KeyCode.R then
		if Ammoo() > 1 then
			Reload()
		end
	end
end)]]

this one is with me positioning another one with ray.Direction


the point closest to my mouse is accurate while the on in the corner isnt

also the ray direction is correct cause i did try it

Your direction is looking a bit off shouldn’t be position, it’s not mathematically correct.

that doesnt cause see the new image i added
only change i did was here

Tool.Activated:Connect(function()
	if Ammoo() > 0 then
		Ammo.Value -= 1
		local wp = Handle.StartAt.WorldPosition
		local dir
		Handle.StartAt.Position += Vector3.new(0,0,Ran.Value)
		dir = Handle.StartAt.WorldPosition
		Handle.StartAt.Position -= Vector3.new(0,0,Ran.Value)
		local bulletray = Ray.new(wp,dir)
		local part,pos = workspace:FindPartOnRay(bulletray,Handle,true,false)
		point(pos,"Hit")
		point(bulletray.Direction,"Dir")
	else
		Reload()
	end
end)

and it looks off cause when i used cframe it also glitched making the part spawn like 150 more on the Z axis

You should also visualize the raycast to see if it is correct, this one should have the proper maths behind it.


this is how it goes

local RcP = RaycastParams.new()
RcP.FilterDescendantsInstances = {Handle}

Tool.Activated:Connect(function()
	if Ammoo() > 0 then
		Ammo.Value -= 1
		local wp = Handle.StartAt.WorldPosition
		local dir
		Handle.StartAt.Position += Vector3.new(0,0,Ran.Value)
		dir = Handle.StartAt.WorldPosition
		Handle.StartAt.Position -= Vector3.new(0,0,Ran.Value)
		local bulletray = workspace:Raycast(wp,dir,RcP)
		local part,pos = bulletray.Instance,bulletray.Position
		visualize(wp,dir)
		visualize(wp,pos)
	else
		Reload()
	end
end)

@dthecoolest are you there?

Well either way the direction should be this, the end position minus the starting position. You could also look at a tutorial for it.

dir = (Mouse.Hit.Position - Handle.StartAt.WorldPosition)*1000

wai wa THAT WORKED OMG THANK YOU SO MUCH!!

1 Like