Using mouse location for raycasting provides innacuracy

I’m trying to make a system where if I press q or e, if my mouse is hovering over a part, it will set my mouse’s position to that part at the location of my mouse. This is not what happens and sometimes my mouse may go somewhere completely different. Help would be appreciated. Also sometimes I may have my mouse dead centered on a part and it still won’t pick up anything. I highlighted where the ray comes out of.


I have the raycast params set to only include these pillars shown on screen So I don’t think there is anything interfering with the ray.

In this picture you can see where my cursor is located but the other pillar turns red instead. Meaning the ray somehow went to that pillar?

local player = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local uis = game:GetService("UserInputService")
local mouse = player:GetMouse()
uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		local realhook = player.Character.odmgear.LeftHook
		local lefthook = player.Character.odmgear.LeftHook.ShootAttachment.Position
		local destination = mouse.Hit.Position
		print(lefthook)
		print(destination)
		local direction = destination-lefthook
		print(direction)
		
		local params = RaycastParams.new()
		params.FilterType = Enum.RaycastFilterType.Include
		params.FilterDescendantsInstances = {workspace.Obstacles}
		
		local raycastResult = workspace:Raycast(lefthook, direction, params)
		
		if raycastResult and raycastResult.Instance then
			print(raycastResult)
			print(raycastResult.Instance)
			print(raycastResult.Instance.Color)
			raycastResult.Instance.Color = Color3.new(1,0,0)
			realhook:BreakJoints()
			print(player.Character.odmgear.LeftHook)
			print(raycastResult.Position)
			realhook.Position = raycastResult.Position
			print(realhook.Position)
			--player.Character.odmgear.LeftHook.Anchored = true
		end
		--RS.Events.Shoot:FireServer(mouse.hit,mouse.Target)
	elseif input.KeyCode == Enum.KeyCode.E then 
	end
end)

ignore the commented out parts I’m still doing troubleshooting. I have only been working with pressing q for now that’s why E is empty. If you want me to provide videos I can. All help will be appreciated. Thank you!

3 Likes

Well first, confirm Direction is pointing the right way, by placing a small part at the player’s position, + Direction*5. This will show if the raycast is going the right way. If it isn’t, I would assume it has something to do with the tool’s attachment position.

1 Like

what would i use as the players position? its humanoidrootpart?

1 Like

Yes you would. This should just place a small part in the direction of the mouse.

I dont think it worked correctly. It spawned the part way out into nothingness even when the ray was accurate

local realhook = player.Character.odmgear.LeftHook
		local lefthook = player.Character.odmgear.LeftHook.ShootAttachment.Position
		local destination = mouse.Hit.Position
		print(lefthook)
		print(destination)
		local direction = destination-lefthook
		print(direction)
		
		local part = Instance.new("Part")
		part.Name = "OMGPARTSOBIG"
		part.Parent = workspace
		part.Position = player.Character.HumanoidRootPart.Position + direction*5

Okay, new method. Confirm the positions of the mouse and the left hook are right. I am thinking the lefthook’s position isn’t working as intended or something.

I believe this is where the issue lies. Assuming ShootAttachment is an Attachment, the Position property is the position of the Attachment relative to the part, not Workspace. To fix this, use Attachment.GlobalCFrame.Position or Attachment.WorldPosition to get the global position of the left hook’s attachment.

Hope this helps!

sadly did not work :frowning: Im going to try the other method provided and report back :saluting_face:

i am using raycasting instead of mouse.hit just for that extra little accuracy.

On this line I assign the hook attachment position to the raycast results position. are you saying you want me to check if the position was actually changed?

No, just confirm that the left hook and the mouse position are in the right positions when you raycast.

local realhook = player.Character.odmgear.LeftHook
		local lefthook = player.Character.odmgear.LeftHook.ShootAttachment.Position
		local destination = mouse.Hit.Position
		print(lefthook)
		print(destination)
		local direction = destination-lefthook
		print(direction)

Confirm that all of these are in the right positions.

ok the mouse.hit is always on target so I’m now like 99% sure its a raycasting issue. I basically just created parts where all the positions were and I found the direction position all the way out in the void. so safe to say we found our problem? Now how do we solve it. :thinking:

Actually, I just remembered that an attachment has a world position, and a local position. Make sure you specify .WorldPosition

local lefthook = player.Character.odmgear.LeftHook.ShootAttachment.WorldPosition

I think this should fix the issue, but i’m not sure.

someone previously already reccomended this and it didn’t really help

If you wanna take a look again this is my current script

local player = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local uis = game:GetService("UserInputService")
local mouse = player:GetMouse()
local HRP = player.Character.HumanoidRootPart
--local attach0:Attachment = Instance.new("Attachment", player.Character.HumanoidRootPart)
--local LinearV = Instance.new("LinearVelocity", player.Character.HumanoidRootPart)
--LinearV.MaxForce = math.huge
--LinearV.Attachment0 = attach0
--LinearV.Enabled = false
local LineF = Instance.new("LineForce", HRP)
uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		local realhook = player.Character.odmgear.LeftHook
		local lefthook = player.Character.odmgear.LeftHook.ShootAttachment.WorldPosition
		local destination = mouse.Hit.Position
		print(lefthook)
		--local part1 = Instance.new("Part", workspace)
		--part1.Anchored = true
		--part1.Position = lefthook
		print(destination)
		local part2 = Instance.new("Part", workspace)
		part2.Anchored = true
		part2.Position = destination
		local direction = destination-lefthook
		print(direction)
		local part3 = Instance.new("Part", workspace)
		part3.Name = "SUPERBIUGPART"
		part3.Anchored = true
		part3.Position = direction
		--LinearV.Enabled = true
		--LinearV.VectorVelocity = CFrame.new(player.Character.HumanoidRootPart.Position, mouse.Hit.Position).LookVector * 50
	
		local params = RaycastParams.new()
		params.FilterType = Enum.RaycastFilterType.Include
		params.FilterDescendantsInstances = {workspace.Obstacles}
		
		local raycastResult = workspace:Raycast(lefthook, direction, params)
		
		if raycastResult and raycastResult.Instance then
			print(raycastResult)
			print(raycastResult.Instance)
			print(raycastResult.Instance.Color)
			raycastResult.Instance.Color = Color3.new(1,0,0)
			realhook:BreakJoints()
			print(player.Character.odmgear.LeftHook)
			print(raycastResult.Position)
			realhook.Position = raycastResult.Position
			print(realhook.Position)
			--player.Character.odmgear.LeftHook.Anchored = true
		end
		--RS.Events.Shoot:FireServer(mouse.hit,mouse.Target)
	elseif input.KeyCode == Enum.KeyCode.E then 
	end
end)

Okay, for more information, could you tell me some more about where you are next to the pillar? Will it raycast to the other pillar, no matter where your mouse is? If you move your character around, will it still always hit that pillar?
Can you try having a massive background part behind the pillars, make it so it can be checked with raycasts, and then every time you press Q, check the raycast.hit position, and set the “SUPERBIUGPART” to that position, so you can see where the raycast ends up hitting in various different positions?

alr answering your first question, the times it doesn’t move the object is because the raycast isn’t picking up anything as seen with the raycast direction bug. using the background you recommended the background actually seemed to end up acting the same way as all the other pillars. I would sometimes purposely aim at the background just for it to not do anything.

and also after further testing it seems completely random whether or not I will hit the pillar. any pillar for that matter. I don’t know if player orientation has anything to do with it since the raycastparams only include the pillars and the background.

i also know that the times when the part doesn’t appear the raycast isn’t finding anything as I have the raycastresult print the part it finds and the times the raycasts don’t appear are the times I don’t get a response in the output

Can you send some pictures of the background, and where the mouse and “SUPERBIUGPART” are?

ill save us some time rq. superbiugpart is currently set to the raycast position. the raycast only returns if there is a position it finds. so if the raycast finds something, the part will normally be at my mouse position except for some random times it isn’t. When the raycast doesn’t hit anything. The part is located at (0,0,0)