Raycast don't seem to work correctly

I’m trying to make a gun module, using of course, raycast, but when i try to shoot looking from a certain angle it does not work.

But when i was trying to debug it, i made a part to see where the mouse was hitting, idk, and with this part, the code seems to work fine, it can shoot from every angles without bugging like without it.

I really dont know how to counter it .,.

The code >>

["Use"] = function(WeaponModel:Tool,MouseHitPos:Vector3)
			local Data = Util.UtilFunctions.ReturnReqData(WeaponModel)
			--Shooting
			if Data.Shared.CanBeUsed.Value == true then
				Data.Shared.CanBeUsed.Value = false 

				if Data.Values.Ammo.Value > 0 then
					Events.UseTool:FireClient(Data.Player,"Use")
					--Fire
					for _,firepoint in WeaponModel:WaitForChild("Handle"):GetChildren() do
						if firepoint:IsA("Attachment") then
----------------------------Raycast
							local Direction = MouseHitPos-firepoint.WorldPosition
							print("--------DIRECTION--------")
							print(Direction)

							-- works when this line is here
							game.Workspace.P2.Position = MouseHitPos  
					
							local RayParams = RaycastParams.new()
							RayParams.FilterType = Enum.RaycastFilterType.Exclude
							RayParams.FilterDescendantsInstances = {Data.Player.Character,WeaponModel}

							local Raycast = game.Workspace:Raycast(firepoint.WorldPosition,Direction,RayParams)
							
							if Raycast then
								warn("Shot confirmed")
								Data.Values.Ammo.Value -= Data.Values.BulletRate.Value
								local p = Instance.new("Part",game.Workspace)
								local hl = Instance.new("Highlight",p)
								p.Size = Vector3.new(Raycast.Distance)
								p.Position = Raycast.Position
								hl.Adornee = p
								p.Anchored = true
								p.Size = Vector3.new(0.1,0.1,0.1)

							else
								warn("Shot cancelled")
							end
----------------------------Raycast
						end
					end
				else
					--tick sound no ammo
					warn("no ammo")
				end
				
				task.wait(Data.Values.Rate.Value)
				Data.Shared.CanBeUsed.Value = true 
			else
				warn("Cooldown")
			end
		end,

Without the line:

With the line:

Change

local Direction = MouseHitPos-firepoint.WorldPosition

with

local Direction = (MouseHitPos-firepoint.WorldPosition) * 1000

Change the “1000” with what ever range you want the raycast to go.

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