0 votes

I've been reading the docs and (once again) I cannot find a method that returns a vector3 representing the direction a spatial node is facing. I was trying to use getglobaltransform().basis, but apparently that doesn't return a vector3? How can I find a Vector3 representing the facing direction of an object?

in Engine by (15 points)

2 Answers

+2 votes
Best answer

There is no "official" forward direction. Which way is forward depends on how you built your mesh. So if you want the foward direction in global coordinates you'll have to use get_global_transform().basis.x, get_global_transform().basis.y or get_global_transform().basis.z, depending on which direction is supposed to be forward.

Don't get confused by the names: the basis is a matrix, and a matrix is a set of factors that are applied to the x-, y- and z-component of a vector that is multiplied with that matrix. So basis.x is the factor for the x-component, and that factor is a Vector3.

by (1,120 points)
selected by
0 votes

Here is what I came up with rotation and movement...

 extends KinematicBody

   var movement = Vector3(0,0,0)
var dir_x = Vector3(0,0,0)
var dir_z = Vector3(0,0,0)


func _physics_process(delta):
    dir_x = get_transform().basis.x
    dir_z = get_transform().basis.z

    if(Input.is_key_pressed(KEY_W)):
        #Forward
        movement = -dir_z
        move_and_slide(movement)

    if(Input.is_key_pressed(KEY_S)):
        #Back
        movement =  dir_z
        move_and_slide(movement)

    if(Input.is_key_pressed(KEY_A)):
        #Left
        movement = Vector3(-1, 0 , 0)
        rotate_object_local(Vector3(0, -1, 0), delta) 
        move_and_slide(movement)

    if(Input.is_key_pressed(KEY_D)):
        #Right
        movement = Vector3(1, 0, 0)
        rotate_object_local(Vector3(0, 1, 0), delta) 
        move_and_slide(movement)


    pass
by (14 points)
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.