In general, those are the equations for calculating points on a line:
x3 = x1+step*(x2-x1)
y3 = y1+step*(y2-y1)
where x1, y1
refer to the first point coords, x2, y2
to the second point, and x3, y3
to the calculated point according to the step
(that can be between 0 and 1, 0 is the start, 0.5 the middle and 1 the end).
Now depending on if you’re using Scale
or Offset
for your UDim2
the implementation is different(assume p1
and p2
are your UDim2s
and step
is your chosen value between 0 and 1):
For UDim2 using scale:
local x3 = p1.X.Scale+step*(p2.X.Scale-p1.X.Scale)
local y3 = p1.Y.Scale+step*(p2.Y.Scale-p1.Y.Scale)
local p3 = UDim2.fromScale(x3, y3)
For UDim2 using offset:
local x3 = p1.X.Offset+step*(p2.X.Offset-p1.X.Offset)
local y3 = p1.Y.Offset+step*(p2.Y.Offset-p1.Y.Offset)
local p3 = UDim2.fromOffset(x3, y3)
For UDim2 using both:
Here you have 2 options:
- Convert one metric(scale or offset) to the other one so you can apply one of the above(you can search how to convert scale to offset or offset to scale in the forums).
- Apply both of the above at once:
local x3sca = p1.X.Scale+step*(p2.X.Scale-p1.X.Scale)
local y3sca = p1.Y.Scale+step*(p2.Y.Scale-p1.Y.Scale)
local x3off = p1.X.Offset+step*(p2.X.Offset-p1.X.Offset)
local y3off = p1.Y.Offset+step*(p2.Y.Offset-p1.Y.Offset)
local p3 = UDim2.new(x3sca, x3off, y3sca, y3off)
" according to the step
(that can be between 0 and 1, 0 is the start, 0.5 the middle and 1 the end)."
That is how lerp works? I want it to be constant here, so step size is like 1/10 of the screen, and it will always be like that