+2 votes

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 translateobjectlocal() 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?

asked Jan 21, 2018 in Engine by mstfacmly (27 points)
edited Jan 22, 2018 by mstfacmly

1 Answer

+2 votes

Normally a translation in an object should be always local. :-) 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)

answered Jan 21, 2018 by wombatstampede (3,193 points)

That worked excellently! Thank you!

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

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

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 How to use this Q&A? before posting your first questions.