Anchoring not working

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By EIREXE
:warning: Old Version Published before Godot 3 was released.

I’m trying to anchor a button to scale horizontally with the viewport, and I’m using stretching mode disabled, however my control does not seem to respond to anchors at all, it’s anchored as “End” to the right, yet when I scale it horizontally it just stays there.

I think this little tidbit should have been included in the Godot documentation about anchors & margins. Would have saved me the past several hours trying to get things to work correctly parented with Node2D or Control nodes.

Thanks for the heads-up!!
Garrett

GarrettGemini | 2018-05-07 23:43

:bust_in_silhouette: Reply From: supaiku

Anchors work fine for me as long as I put all GUI nodes inside a CanvasLayer.

Oh right the CanvasLayer, I completely forgot about that, I’m deep today, thanks!.

EIREXE | 2016-07-24 22:50

Thank you!! I was fumbling around for quite some time trying to get the anchors to work before finding your comment.

ChildLearning.Club | 2022-10-20 00:27

:bust_in_silhouette: Reply From: socheat.khauv

It is working fine for me, I think you might ran into the same problem as me.

Anchor is working only if that ui control is child of ConvasLayer. if it is not child of ConvasLayer then it will show incorrectly for different screen size.

the best scene hierarchy is

Node2D (root)

  • ConvasLayer (HUD)
    • ConvasItem (Button, Text, etc…)
  • Game Objects (sprite)
:bust_in_silhouette: Reply From: kennychidot

I experienced this quite often till I realized the problem was that I was resizing UI elements that were not the root node.Resizing a child node affects the anchoring as the parent node still keeps the original scale of the child node.So if you need to resize your UI element do it ONLY with the Root node of your scene.