Messing with camera position makes mouse position inaccurate

I’ve been working on a grapple system in which when the player presses Q and their camera’s CFrame will be changed. If they press Q again, their camera will be reset and they will grapple is a vantage point is availabe at the position of their mouse. Everything seems to work well except for the fact that the mouse position is inaccurate. I have deduced the changing of the camera’s CFrame to be the issue as once I disable any scripts relating to the camera, all works well. Any help would be EXTREMELY appreciated.

External Media External Media

Grapple Code:
(mousepos exists at the top of the script and is defined by mouse.Hit.Position

if script.Parent.GrappleCam.Initial.Value then

			hb = game:GetService('RunService').Heartbeat:Connect(function()
				if script.Parent.GrappleCam.Initial.Value then
					grappleStartIKTarget.CFrame = CFrame.new(mouse.Hit.Position, root.Position) * CFrame.Angles(0, math.rad(180), 0)
				else
					grappleStartIK:Destroy()
					grappleStartIKTarget:Destroy()
					grappleStartIdleAnim:Stop()
					root.Anchored = false
					cam.CameraType = Enum.CameraType.Custom
					cam.CameraSubject = char.Humanoid
					hb:Disconnect()
				end
			end)
		else
			local rayresult = game.Workspace:Raycast(root.Position, (mousepos - root.Position) * 200, blacklist)

			if rayresult and rayresult.Instance then
				if rayresult.Instance == target then
					mousepos = mouse.Hit.Position
					if (mousepos - root.Position).Magnitude > 10 and mousepos.Y > root.Position.Y + 15 then
						script.CheckSpace.Value = 'None'
						hook()
					end
				end
			end
			
		end	         

Grapple Camera Code:

if not script.Initial.Value then
			script.Initial.Value = true
		else
			script.Initial.Value = false
		end
		
		heartBeat = game:GetService('RunService').Heartbeat:Connect(function()
			if script.Initial.Value then
				local maxRotX = 60
				local maxRotY = 30

				local overTheShoulder = head.CFrame + (root.CFrame.LookVector * -4) + (root.CFrame.RightVector * 2) 
				cam.CameraType = Enum.CameraType.Scriptable
				cam.CFrame = overTheShoulder * CFrame.Angles(
					math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxRotY),
					math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxRotX),
					0
				) -- y is placed in front of x because when it comes to camera movements, the y-axis is side-to-side and the x-axis is up-and-down. think of a 2d graph (which would be how a mouse moves), y is horizontal and x is vertical. now apply it to a 3d world, you're going to need to use the y coordinates for the x coordinates and vice-versa. 

				if script.Initial.Value then
					root.Anchored = true
				end
			else
				heartBeat:Disconnect()
			end
		end)
1 Like

To anyone struggling with the same issue, I found a solution after troubleshooting for hours. In the grapple code, I changed the heartbeat to a renderstepped since renderstepped is much better for input-related situations. If this doesn’t work, try getting rid of the “mouse.hit.position” and replacing it with a raycast with a direction of the camera to the mouse location.

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