Translate object using local co-ordinates (Solved)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mstfacmly
:warning: Old Version Published before Godot 3 was released.

Trying to make a character skip a distance by pressing a button. Example code:

if Input.is_action_just_pressed("action"):
    translate(Vector3(0,0,2))

The translate function is supposed to work in local co-ordinates, but it seems to move the character along the global Z axis.

I’m using Godot 3.0, which has the translate_object_local() function as well, but I have the same problem with it.

I tried combining things with Basis, including using get_transform().basis[0], no functional result.

Any ideas?

:bust_in_silhouette: Reply From: wombatstampede

Normally a translation in an object should be always local. :slight_smile: It is local to its parent object. But that probably is not what you search.

This should do the job:

func do_translateZ(dist):
	var localTranslate = Vector3(0,0,dist)
	translate(get_transform().basis.xform(localTranslate))

I took this from my forum post: “Godot 3D Vector/Physics Cheat-Sheet:”
https://godotdevelopers.org/forum/discussion/18480/godot-3d-vector-physics-cheat-sheet

The post is basically for Godot 2.x but 3d vector calcs should stay the same. (Just the demo project wouldn’t run 1:1)

That worked excellently! Thank you!

I think I’ll bookmark your forum thread, or it should be updated and added to the official docs!

mstfacmly | 2018-01-22 09:06

I just used this in Godot 3.0.x, except with the y-axis, and it worked like a charm. Thanks for the post!

Zakkle | 2018-05-04 01:57

Just a heads up, the URL has changed:

https://godotforums.org/discussion/18480/godot-3d-vector-physics-cheat-sheet

stormwarestudios | 2020-08-21 18:23