How to align player rotation with a movement vector?

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

i’ve created a vector, rotated the vector and used to walk, but i do not know how to rotate player to where vector is pointing to
I’ve tried something like: player.look_at(moveto, Vector3(0,1,0)) but did not work
Thanks

extends Spatial
var vetormov= Vector3(0.5,0,0.5)

func _process(delta):

   var moveto
    if(Input.is_key_pressed(KEY_RIGHT)): angulo = angulo -2	
    if(Input.is_key_pressed(KEY_LEFT)): angulo = angulo +2
    
    # rotate vector--------------------
    moveto = vetormov.rotated(Vector3(0, 1, 0),deg2rad(angulo))
    
    # move player using vector---------
    if(Input.is_key_pressed(KEY_UP)):	
           var t = player.get_transform()
           t.origin += moveto /10
	   player.set_transform(t)
:bust_in_silhouette: Reply From: BraindeadBZH

Why you don’t just apply the rotation to your player transform? Transform also has a rotated function.

thanks for the answer
I’ve tried:
player.global_transform.basis.z= moveto

here player is just a box, when it rotates it get deformed in a certain angle

also tried:

t_rot = t.rotated(Vector3(0,1,0),deg2rad(angulo))
		player.set_transform(t)
		player.set_transform(t_rot)

Still not working

clerison | 2019-07-30 22:25

I don’t understand why you have two transforms? The basis of a transform is to set all the transform once in an unified manner.

So what’s not working? you mean your rotation is not correct? There is no reason that the function rotated is not working as expected.

BraindeadBZH | 2019-07-30 22:56

the mesh is deforming in rotation
enter image description here

if(Input.is_key_pressed(KEY_UP)):
		var t = player.get_transform()
		t.origin += moveto /10
		t.basis.z= moveto.normalized()
		player.set_transform(t)

clerison | 2019-07-31 11:08

Thanks this is clearer like that. You can’t modify a Basis like that. A Basis is matrix which combine the rotation and scaling, so what you see is normal: read

As the Godot doc already contains useful information, I invite you to read this.

BraindeadBZH | 2019-07-31 11:25

thanks, i’ve got it, since you’ve told me the Basis is not the way i’ve concentrated my efforts in create a vector ahead and “look” at this

var moveto = Vector3(0,1,0)

if(Input.is_key_pressed(KEY_RIGHT)):angulo = angulo -2
if(Input.is_key_pressed(KEY_LEFT)): angulo = angulo +2

#rotate vector---------------------------------------
moveto = vetormov.rotated(Vector3(0, 1, 0),deg2rad(angulo))

# player turn and align to vector-------------
var look = Vector3(player.global_transform.origin.x+(moveto.x/1000),0,player.global_transform.origin.z+(moveto.z/1000))
player.look_at(look,Vector3(0, 1, 0))

# move with vector --------------
if(Input.is_key_pressed(KEY_UP)): player.move_and_slide(moveto*velplayer)

Now i can create a vector and make the player follow the vector
and i can create player body and make the vector follow the player : var facing = player.global_transform.basis.z

clerison | 2019-07-31 14:24