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.