Making raycast go past mouse

I’m trying to make a top-down shooter and working on the framework for the weapon system, but I ran into a problem I forgot how to fix and can’t find on DevForum. I want to make a raycast go to the mouse, but I want it to go past it, and I can’t figure out how to do that from a top-down perspective. I’m also unable to use the character’s root part because I want it to be accurate.
What I want:

What I get:

Code:

userinputservice.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local function raycast(origin,point)
			local distance = (origin - point).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(origin, point)*CFrame.new(0, 0, -distance/2)
			p.Parent = game.Workspace
		end
		local root = character.HumanoidRootPart
		local origin = root.Position + root.CFrame.LookVector * 2
		local point = Vector3.new(mouse.Hit.X, origin.Y, mouse.Hit.Z)
		local raycastResult = workspace:Raycast(origin, point)
		if raycastResult then
			raycast(origin,raycastResult.Position)
		else
			raycast(origin,point)
		end
	end
end)

I’m lost and don’t know how to achieve this and any help would be appreciated, thanks.

It looks like you are firing the ray at the position the mouse is hitting the baseplate.
Instead of firing the ray to that Position, fire it to the X and Y of that Position, but set the Z to the same height as the players RootPart. That way it won’t fire from the Player down to the baseplate.

Its not hitting the ground its firing towards the mouse position at the same y position as the root so its making it and not hitting the ground bht its only going to where the mouse is and i want it to go past the mouse

Try getting the direction of the mouse relative to the player

local dir = (point - origin).Unit
1 Like