How do I get an Area2D to move other objects in a scene with code?

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

I can’t figure out how to get an Area2D to move other objects in the scene with code using the area_entered signal. I think the code would maybe need a transform 2D?

Why can’t you just change the position of the area that entered it?

exuin | 2021-02-25 04:48

Because that’s exactly what I don’t know how to do.

703337 | 2021-02-25 05:13

:bust_in_silhouette: Reply From: exuin

The area_entered signal has the area that entered as an argument. In your _on_area_entered(area) function, or whatever you called it, just change the position of the area with

area.position = new_position

I’m not exactly sure what to put into the space where new_position is. I can only seem to put in a single number before it stops working.

703337 | 2021-02-25 06:57

Position is a Vector2. So you’ll need to put a value of that type in there.

exuin | 2021-02-25 06:58