aligning an area2D to a node2d?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster

Hi all, I want to exactly align an Area2D to a node2D.

The problem is the area 2D has been SCALED, so when I do this:

$Area1.global_position = $node2D1.global_position

It aligns but it’s centred in the top left corner of the node2D.

How can I find a way to offset the area2D so the the top left of it exactly aligns to the top left of the node2d?

:bust_in_silhouette: Reply From: Zylann

First of all, a Node2D and Area2D both don’t have a size. What you perceive as size is either the sprite or the collision shape they have.
They don’t align because your collision shape is centered but the child of your other Node2D is not.

I see two ways to center tour area:

  1. When you assign the position of the area, add an offset equal to half the size of your area, half the size of the target node2d, or an average of both if their size differ

  2. Design your game objects such as their root is centered instead of being top-left