Aligning mouse with SurfaceGui

I am trying to align the player’s mouse with the computer’s mouse. However the mouse is not aligning correctly. I’ve tried multiple ways of aligning the mouse but nothing works.

For the demonstration I used the X axis only, however I am looking to align both 2D positions.

Here’s my code:

local resScreenGui = Instance.new("ScreenGui")
resScreenGui.Parent = localPlayerGui

game:GetService("RunService").RenderStepped:Connect(function()
	
	local mousePos = UserInputService:GetMouseLocation()
	local xDiff = resScreenGui.AbsoluteSize.X - newScreenSurface.AbsoluteSize.X
				
	mouseIcon.Position = UDim2.fromOffset(mousePos.X - xDiff/2, 0)
	
end)
1 Like

Ah, I know this. I had this exact same issue. You need to use raycasts so that you can properly reference where the mouse hits on the part.

  function ToggleScroll(b) 
        local CurrentZoom=(workspace.CurrentCamera.CoordinateFrame.p - player.Character.Head.Position).magnitude
        player.CameraMaxZoomDistance,player.CameraMinZoomDistance = ((not b and CurrentZoom) or MaxZoomCache),((not b and CurrentZoom) or MinZoomCache)
    end  

uis.InputChanged:Connect(function(input)
            if input.UserInputType == Enum.UserInputType.MouseMovement then
                local ray = workspace.CurrentCamera:ScreenPointToRay(mouse.X, mouse.Y)
                local params = RaycastParams.new()
                params.FilterType = Enum.RaycastFilterType.Whitelist
                params.FilterDescendantsInstances = {cursorscreen}
                local hit = workspace:Raycast(ray.Origin, ray.Direction * 1000,params)

                if hit then
                    uis.MouseIconEnabled = false

                    ToggleScroll(false)

                    local x = 
                        surfacegui.AbsoluteSize.X-
                        ((-cursorscreen.CFrame:PointToObjectSpace(hit.Position) + cursorscreen.Size/2) * 
                        surfacegui.AbsoluteSize.X/cursorscreen.Size.X).X
                    
                    local y = ((-cursorscreen.CFrame:PointToObjectSpace(hit.Position) + cursorscreen.Size/2) * surfacegui.AbsoluteSize.Y/cursorscreen.Size.Y).Y

                    cursorFrame.Position = UDim2.new(0,x,0,y)

                    owner.mRe:FireServer(x,y)
                else
                    uis.MouseIconEnabled = true

                    ToggleScroll(true)
                end
            end
        end)

I just took this code out of my macos project but change the cursorscreen to your computer screen part . This was also made to replicate, so in your case you can just remove the owner.mRe line and use and just use x, y from there. I’ve also thrown in a bonus toggle scroll feature that will dynamically disable the scrolling of camera zooming when on the screen.

4 Likes

I’m sorry I just wanted to know but is there a reason why you wanna do this? Like, why not just change the cursor’s image whenever it enters the SurfaceGui?

1 Like

Then it doesn’t look cool. The cursor won’t look like it’s actually being controlled on a screen, and you’ll be able to see it as a 2d image in a 3d world with perspective. Plus, if the camera is like far out then the cursor will look huge or if its zoomed in then it will look really small.

1 Like

Works pretty well however I’m getting the inverse.
Setting it to negative does not seem to work either.

https://gyazo.com/1b4ffd21afb18e1a7aaf6df08bc785ef

1 Like

try this

 local x = 
                        surfacegui.AbsoluteSize.X-
                        ((cursorscreen.CFrame:PointToObjectSpace(hit.Position) + cursorscreen.Size/2) * 
                        surfacegui.AbsoluteSize.X/cursorscreen.Size.X).X
2 Likes

Cant believe I overlooked that. Thanks for your help.

1 Like

No worries, I happened to be also making a computer but gave up some OOP mess.

1 Like

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