0 votes

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

in Engine by (15 points)

1 Answer

+1 vote

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.

by (21,973 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.