How to make pupils on a Surface Gui follow the mouse?

Hello, I want to make this character’s eyes follow the player’s mouse, but I’m not exactly sure how to calculate it. I only have the Right Eye set up for now

local RE = RS:WaitForChild("ReplicatedEvents")
local test = RE:WaitForChild("DevEggMaker")
local players = game:GetService("Players")

local player = players.LocalPlayer

local TS = game:GetService("TweenService")
local TweenEyeInfo = TweenInfo.new(
	0.1,
	Enum.EasingStyle.Sine
)

local playerInput = game:GetService("UserInputService")
local myegg = game.Workspace:WaitForChild("EggMaker")

local eggLooking = false


--
local maxXmovement = 0
local maxYmovement = 0

local originalPosRight = Vector2.new(0,0)

local function updateEggUIValues()
	local values = myegg:WaitForChild("Values")
	local eyeValues = values:WaitForChild("Eyes")
	maxXmovement = eyeValues.MaxXMovement.Value
	maxYmovement = eyeValues.MaxYMovement.Value
	originalPosRight = myegg["Right Eye"].SurfaceGui.EyeWhite.RightPupil.Position
	originalPosRight = Vector2.new(originalPosRight.X, originalPosRight.Y)
end

--look at where the player taps
local tapConnection = nil
local function eggStartLookingAtTouch()
	tapConnection = playerInput.TouchTap:Connect(function(location)
		if eggLooking == false then
			tapConnection:Disconnect()
		end
		
		
	end)
end

--look at where the player's mouse moves
local function eggStartLookingAtMouse()
	if not playerInput.MouseEnabled then
		eggStartLookingAtTouch() --fallback if user has touchscreen
		return
	end

	local eggLeftEye = myegg:WaitForChild("Left Eye")
	
	local eggRightEye = myegg:WaitForChild("Right Eye")
	local eggRightPupil = eggRightEye.SurfaceGui.EyeWhite.RightPupil
	
	
	local mouse = player:GetMouse()

	local lastXPosOfMouse = 0
	local lastYPosOfMouse = 0
	
	while eggLooking do
		if mouse.X ~= lastXPosOfMouse or mouse.Y ~= lastYPosOfMouse then
			lastXPosOfMouse = mouse.X
			lastYPosOfMouse = mouse.Y
			
			local mousePos = Vector2.new(mouse.X, mouse.Y)
			local direction = 0 --what do i do
			
			local moveY = math.clamp(direction.X, -maxXmovement, maxXmovement)
			local moveX = math.clamp(direction.Y, -maxYmovement, maxYmovement)
			
			TS:Create(eggRightPupil, TweenEyeInfo, {Position = UDim2.new(moveX, 0, moveY)}):Play()
			
			
		end
		task.wait()
	end
	
end

--update the egg we are looking at
test.OnClientEvent:Connect(function()
	print("Testing eye functionality")
	eggLooking = true
	updateEggUIValues()
	eggStartLookingAtMouse()
end)

I don’t exactly understand the math here as I am using Size and not Offset.

bump bump bump bump bump bump bump

This will probably maybe work (If this doesn’t work then I have no clue)


local pupilAbsPos = eggRightPupil.AbsolutePosition
local pupilAbsSize = eggRightPupil.AbsoluteSize
local pupilCenter = pupilAbsPos + (pupilAbsSize / 2)

local mousePos = Vector2.new(mouse.X, mouse.Y)
local direction = (mousePos - pupilCenter)

if direction.Magnitude > 0 then
	direction = direction.Unit
else
	direction = Vector2.new(0, 0)
end

local moveX = math.clamp(direction.X * maxXmovement, -maxXmovement, maxXmovement)
local moveY = math.clamp(direction.Y * maxYmovement, -maxYmovement, maxYmovement)
TS:Create(
	eggRightPupil,
	TweenEyeInfo,
	{Position = UDim2.new(0, moveX, 0, moveY)}
):Play()

Thanks a lot! The script you gave me didn’t fully work, but with a little bit of brain hurting and desmos, I was able to fix it!!

local function eggStartLookingAtMouse()
	if not playerInput.MouseEnabled then
		eggStartLookingAtTouch() --fallback if user has touchscreen
		return
	end

	local eggLeftEye = myegg:WaitForChild("Left Eye")
	
	local eggRightEye = myegg:WaitForChild("Right Eye")
	local eggRightPupil = eggRightEye.SurfaceGui.Frame.EyeTexture
	
	
	local mouse = player:GetMouse()

	local lastXPosOfMouse = 0
	local lastYPosOfMouse = 0
	
	
	local pupilAbsPos = eggRightPupil.AbsolutePosition
	local pupilAbsSize = eggRightPupil.AbsoluteSize
	local pupilCenter = pupilAbsPos + (pupilAbsSize / 2)

	local screenSize = workspace.CurrentCamera.ViewportSize
	local screenSizeCenter = (screenSize / 2)
	
	
	while eggLooking do
		if mouse.X ~= lastXPosOfMouse or mouse.Y ~= lastYPosOfMouse then
			
			
			local mousePos = Vector2.new(mouse.X, mouse.Y)
			mousePos = screenSizeCenter - mousePos
			
			local direction = mousePos - pupilCenter
			
			if direction.Magnitude > 0 then
				direction = -direction.Unit
			else
				direction = Vector2.new(0, 0)
			end
			
			local rangeX = (maxXmovement - minXmovement) / 2
			local rangeY = (maxYmovement - minYmovement) / 2
			local centerX = (maxXmovement + minXmovement) / 2
			local centerY = (maxYmovement + minYmovement) / 2
			
			local moveX = math.clamp(centerX + (direction.X * rangeX) , minXmovement, maxXmovement)
			local moveY = math.clamp(centerY + (direction.Y * rangeX)  , minYmovement, maxYmovement)

			TS:Create(
				eggRightPupil,
				TweenEyeInfo,
				{Position = UDim2.new(moveX,0, moveY, 0)}
			):Play()
			
			
		end
		task.wait()
	end
	
end

I just calculated the origin of the x and y values to the middle of the player’s viewport. I also changed the calculation for movex and movey because I had to change the clamp from a minimum negative number to a minimum positive number.

Thanks a whole bunch. You’re the best!