Can't move objects via GDScript

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

I’m new to the engine and I’m getting desperate. Even the most basic operation takes hours to be set straight. I just don’t understand this stuff or why, when I finally find the function I’m looking for, looks like it doesn’t work for X reason.

What I’m trying to do:

when I click a specific button, an MeshInstance node is set to a specific mesh and its position should be set relative to the origin.

My code:

func toSphere():
print (a)
self.mesh = SphereMesh.new()
global_translate(Vector3(0, 0, 0))

func toCube():
print (a)
self.mesh = CubeMesh.new()
global_translate(Vector3(0, 0, 0))

func toHead():
print (a)
self.mesh = get_node(“Mesh_Head”).mesh
global_translate(Vector3(-0.574,0.91,0.684))

What happens:

Function “toHead” moves the mesh how it’s supposed to be moved, but when I click the others (that are supposed to move the MeshInstance back to world origin) the MeshInstance just stays there. That also means each time I activate the “toHead” function the MeshInstance adds another offset.
Halp pls.

:bust_in_silhouette: Reply From: BraindeadBZH

First not sure why your are using global_translate, but whatever your are using, global_translate or translate, you have to realize the vector in parameter is a delta, not an absolute position. In other words when your translate with a zero vector this is normal it is not moving because this is what you tell your node to do.

I used globaltranslate because I thought that would give me an absolute position! How can I get an absolute position? Should I use the root or something?
I understand it’s all very noobie but I’m really at text scripting and I’m having kind of a hard time.

LucaFox | 2019-09-06 10:50

If you want to position your node at a specific position instead of translating it, just set the transform.origin vector.

BraindeadBZH | 2019-09-06 11:00

All nodes position are relative to the parent coordinates, the absolute allows to change the position in world coordinates instead but does not change the way a translate is working.

BraindeadBZH | 2019-09-06 11:04

Hey man!
Thanks. I couldn’t find the right function to do that. Coming from Blueprints visual scripting, there are lots of differences I have to understand before becoming truly productive. Tried what you suggested and worked perfectly. Thanks again!

LucaFox | 2019-09-06 14:28