Changing the Sprite2D offset without changing its position in the main scene

Godot Version

4.2

Question

Hello =)
I am trying to display my sprite in the correct order with the “Y sort enable” function. However, the center is in the middle of the sprite. I saw I can correct it by changing the offset of the sprite2D.

But, I need to do it by script. I have an instantiated object that grows thus I need to change its size after instantiation. I tried to adjust the offset ($Sprited2D.offset.y = newvalue).
but by doing so, the sprite changes position in the main scene, and the rigidbody stays at the same place.

How can I change the Sprite2D offset without changing its position in the main scene?

Have you tried decreasing the Sprite2D position by the same adjustment you increase the offset by?

For example:
$Sprited2D.offset.y += newoffset
$Sprite2D.position.y -= newoffset

(If your sprite is scaled, you will need to multiply the position adjustment by the corresponding coordinate scale)

Hello,

Thanks, LastIcarus.

I already tried and just tried again but the offset moved with the new position also.