+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?

in Engine by (29 points)
edited by

1 Answer

+3 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)

by (3,284 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.
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 webmaster@godotengine.org with your username.