Raycast not following mouse correctly

Basically at long ranges the raycast doesn’t work at all, sometimes it doesn’t even work at close range

Everything on the character IS ignored by the raycast as well so it can’t be colliding with it.
It starts at the head and ends at the mouse.Hit.Position area
If you need anymore info simply comment what you need :slight_smile:

local event = game.ReplicatedStorage.SendMouse


event.OnServerEvent:Connect(function(plr, mouse, headP, char)
	local RayOrigin = headP
	local RayDirection = mouse
	local raycastParamas = RaycastParams.new()
	raycastParamas.FilterDescendantsInstances = {char, char:GetDescendants()}
	raycastParamas.FilterType = Enum.RaycastFilterType.Blacklist
	local RaycastResults = workspace:Raycast(RayOrigin, RayDirection, raycastParamas)
	if RaycastResults then
		print(RaycastResults.Instance)
		local distance = (RayOrigin - RaycastResults.Position).Magnitude
		local p = Instance.new("Part")
		p.Anchored = true
		p.CanCollide = false
		p.Size = Vector3.new(0.1, 0.1, distance)
		p.CFrame = CFrame.lookAt(RayOrigin, RayDirection)*CFrame.new(0, 0, -distance/2)
		p.Parent = game.Workspace
		wait(0.2)
		p:Destroy()
		end
end)

(i can’t send the video for some reason, but basically you have to be super close to a wall for the raycast to even activate)

(local script that activates the remote event)

local player = game.Players.LocalPlayer
local char = script.Parent
local mouse = player:GetMouse()
local head = char.Head
local Event = game.ReplicatedStorage.SendMouse
local RS = game:GetService("RunService")

RS.Heartbeat:Connect(function()
	Event:FireServer(mouse.Hit.Position, head.Position, char)
end)

its on a run-service so i can check the raycast 24/7

Try to replace that line with this one

local RaycastResults = workspace:Raycast(RayOrigin, (RayDirection - RayOrigin).Unit * 1000, raycastParamas)
1 Like

thanks! if you don’t mind can you explain how this fixed it all lol

local RayDirection = mouse

Isn’t a direction, to get a directional vector you needed to do the following.

local RayDirection = (mouse - headP).Unit

With this directional vector you’d multiply it by some magnitude (to represent a distance).

RaycastDirection * 1000
1 Like