How do I change the translation of a node using gdscript?

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

I know it’s a noobie question, but how do I change the translation of a node using gdscript?

:bust_in_silhouette: Reply From: kidscancode

You didn’t mention whether this was a 2D or a 3D node. Both have properties and functions for making this happen. See the links I’ve put on each node type below:

  • 2D:

A 2D node inherits from Node2D . You can move the node by changing its position property:

position.x += 100  # moves the object 100 pixels to the right

or by using the translate() function:

translate(Vector2(100, 0))  # same result

Note: both of these are in local coordinates.

  • 3D:

A 3D node inherits from Spatial. You can move by changing the origin property of the object’s transform, or its translation:

transform.origin.x += 10  # moves the object 10 units along the x axis
translation.x += 10  # same result

or again, with the translate() function - although keep in mind if you’ve scaled the object, this will affect the scale of the movement as well:

translate(Vector3(10, 0, 0))

Again, in the body’s local coordinate space.

Important note:
The answer to this question changes if you’re using a physics object, such as a kinematic or rigid body. These nodes have their own methods for controlling movement and shouldn’t be moved using the above methods.

Thank you very much. It turned out it was easier than I had thought.

MDobleZ | 2020-07-27 17:36