Vector3 representing direction an object is facing?

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

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 get_global_transform().basis, but apparently that doesn’t return a vector3? How can I find a Vector3 representing the facing direction of an object?

:bust_in_silhouette: Reply From: Warlaan

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.

:bust_in_silhouette: Reply From: Asmosis

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