Rotate mesh based on the direction of the player character ?

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

It’s 3D and top down…45degree top down with 8 direction movement. When I press left, the player moves left but the mesh should also look left and this, should happen smoothly, for all 8 directions.

It …kinda? works what I have but it is very buggy and only works if the the starting rotation is 180 degree on Y axis. I search for a more reliable and less buggy version but couldn’t come up with one.

it’s this line particular:

	var new_transform = $dachs2.transform.looking_at(direction, Vector3.UP)
    $dachs2.transform  = $dachs2.transform.interpolate_with
   (new_transform, speed * delta)

and the rest of it:

extends KinematicBody

export var speed : float = 10
export var friction : float = .18
var motion = Vector3.ZERO
var velocity : Vector3
export var gravity = Vector3.DOWN * 50
var vel = Vector3()

func _ready():
var direction = Vector3.ZERO

func _physics_process(delta):

velocity += gravity * delta


get_node("dachs2/AnimationPlayer").play("Idle")

var direction = Vector3.ZERO

if Input.is_action_pressed("right"):
	direction.x += 1
if Input.is_action_pressed("left"):
	direction.x -= 1
if Input.is_action_pressed("down"):

	direction.z += 1
if Input.is_action_pressed("up"):
	direction.z -= 1
	
if direction != Vector3.ZERO:
	direction = direction.normalized()
	
var new_transform = $dachs2.transform.looking_at(direction, Vector3.UP)
$dachs2.transform  = $dachs2.transform.interpolate_with(new_transform, speed * delta)

velocity.x = direction.x * speed
velocity.z = direction.z * speed

velocity = move_and_slide(velocity, Vector3.UP)

print(direction)