Get a player mesh to rotate based on the direction of the player character (In 3D)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lavaduder
:warning: Old Version Published before Godot 3 was released.

I’ve been trying to get my character mesh to rotated based on the direction of my character. Like if I move up, it will face up, if I move left it will face left, If I go right and down it will face southeast.

How do I do this

P.S. It’s a spatial project.

Is it a top-down view or what? Otherwise that doesn’t make much sense. Anyway, all you need to do is define a loop in fixed process that rotates the character in each frame, then when you detect the input the rotation value will change depending on the key.

If you could provide at least a screen capture of your scene it would be great.

rredesigns | 2017-06-12 02:00

:bust_in_silhouette: Reply From: mollusca

There are different approaches you can take depending on what kind of game you’re making. A really basic example that assumes the character is moving in the x-z plane and points it in the direction of travel:

var vel = Vector3()

func _fixed_process(delta):
    var tf = get_global_transform()
    if Vector2(vel.x, vel.z).length_squared() > 0.001:
        tf.basis = Matrix3(Vector3(0, 1, 0), atan2(-vel.x, vel.z))
    tf.origin += vel * delta
    set_global_transform(tf)

You might have to change the signs in the atan2 command if your character points the wrong way.