Help tweening to center position

I’m trying to tween my frame to the target frame position but it just isn’t correct. The X axis seems correct but the Y axis is way above the target Y axis.

local TweenService = game:GetService("TweenService")

local guiObject = script.Parent
local targetObject = script.Parent.TargetFrame

local function GetCenter(absoluteSize, absolutePosition, anchorPoint)
	local corner = absolutePosition + (absoluteSize * anchorPoint)
	local center = corner + (absoluteSize / 2)
	return center
end

local function OffsetToScale(gui, offset)
	local absoluteSize = gui.AbsoluteSize

	print(offset.X, absoluteSize.X)

	return UDim2.fromScale(offset.X / absoluteSize.X, offset.Y / absoluteSize.Y)
end

local origin = guiObject.AbsolutePosition

local targetSize = targetObject.AbsoluteSize / 2
local targetPosition = targetObject.AbsolutePosition
local targetAnchorPoint = targetObject.AnchorPoint

targetPosition = GetCenter(targetSize, targetPosition, targetAnchorPoint)

local newPosition = OffsetToScale(gui, targetPosition)

local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
local tween = TweenService:Create(guiObject, tweenInfo, {Position = newPosition})
tween:Play()
2 Likes

check anchor point, it might be different for both frames

Both frames anchorpoints are 0.5, 0.5

1 Like

I’ve tested in studio by manually calculating and it works but when playing, the gui absolutesize changes.

1 Like

yk, there is an option, from what can i see you are using linear easing style, this mean tween is not neccesary, it’s better to use lerp, also easing direction can affect tweening behavior

I don’t think the tweening is the issue.

1 Like

not tweening but easing direction

Tried changing to In but doesn’t make a difference.

1 Like

1 Like

ok what i learned is that you can’t set position to vector2, you have to convert it to UDIM2

It is converted:

local function OffsetToScale(gui, offset)
	local absoluteSize = gui.AbsoluteSize
	return UDim2.fromScale(offset.X / absoluteSize.X, offset.Y / absoluteSize.Y)
end

local newPosition = OffsetToScale(gui, targetPosition)
	
local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
local tween = TweenService:Create(guiObject, tweenInfo, {Position = newPosition})
tween:Play()
1 Like

yk, pretty much idk why you need absolute position, if normal position will do it right

I’m just learning some gui stuff and this would be useful for me.

1 Like

i tested it in studio, absolute position is pretty much pixel size, you can use normal position to achieve this effect and it would be perfect

also abosulte values are given in pixels which is not that correct, if you really want to scale something for all resolutions, use UDIM2 with first number only

I’m using Udim2.FromScale to achieve this is scaled but the Y axis is just wrong. I got IgnoreGuiInset enabled.

1 Like

listen, you don’t need to use Udim2.fromScale, simply do:

local A = script.Parent.A
local B = script.Parent.B

-- some code

local goal = {Position = B.Position}
local info = (your tween info)
local tween = TweenService:Create(A,info,goal)

tween:Play() -- will move frame to perfect frame's position, you can manipulate size if anchor point isn't 0.5,0.5

I know I can do that but if they were in different parents, I would need to use what I’m currently doing.

1 Like

then listen, absolute position is given in pixels, this mean depending on local resolution, to move your frame inside another frame, you have to do:

local goal = {Position = Udim2.new(0,B.AbsolutePosition.X,0,B.AbsolutePosition.Y)}

This works but when play testing, its position isn’t at the goal.

1 Like

it’s strange, for me it works perfectly, it might be cause of wrong positioning, have you used only scale to place gui? or you dragged it freely? if yes then please use scale only for moving it