Here is the code I have right now:
function Util.MapVectorUsingAnchorPoint(position: Vector2, size: Vector2, anchorPoint: Vector2?): Vector2
anchorPoint = anchorPoint or Vector2.new(0.5, 0.5)
if anchorPoint == Vector2.new(0, 0) then
return position
end
local adjustedX = position.X - anchorPoint.X * size.X
local adjustedY = position.Y - anchorPoint.Y * size.Y
return Vector2.new(adjustedX, adjustedY)
end
Unfortunately, this code does not work, the object just stays hogging the top left corner instead of being in the center as it should.
Any help would be appreciated.
P.S. the position and size are 0-1 based vectors
I don’t understand what’s wrong with this piece of code. If you are positioning gui, are you sure you are properly passing the gui’s anchor point and setting it’s anchor point back to 0, 0
?
-- -5, -5
print(MapVectorUsingAnchorPoint(Vector2.new(0, 0), Vector2.new(10, 10, 10)))
-- 0, 0
print(MapVectorUsingAnchorPoint(Vector2.new(0, 0), Vector2.new(10, 10, 10), Vector2.new(0, 0)))
-- -10, -10
print(MapVectorUsingAnchorPoint(Vector2.new(0, 0), Vector2.new(10, 10, 10), Vector2.new(1, 1)))
In my post, I said I am using 0-1 based vectors as my inputs, and that is probably why it is not working. I’ll try using vectors which are not that small.
Sorry, I didn’t really read your response. I want it to appear inside the exact same spot, in the center. But the top left corner, is in the top left corner of the screen. Instead of the center like it should be.