How to move and rotate a KinematicBody(3D) towards target

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

I can´t get this to work. The code I´ve borrowed from other solutions to rotate towards target always fail once I try to move the player.

The one that works best right now is the one from codetuto rotation tutorial, here´s my adaption:

if value < 1:
	var t = player.get_transform()
	var lookDir = target_pos - player_pos
	var rotTransform = t.looking_at(lookDir,Vector3(0,1,0))
	var thisRotation = Quat(t.basis).slerp(rotTransform.basis,value)
	value += delta
	if value>1:
		value = 1
	player.set_transform(Transform(thisRotation,t.origin))

elif value == 1:
	player.move(playerFacing*delta*5)

this rotates first and then moves toward, but still ends up a little on the left side of the target, if I take away the conditional for player.move() then the player rotates to much, ends up to the right of the target, dipping like a boat…

Any help would be very much appreciated. Doesn´t have to be based upon the above code, I just want to learn how to make a smooth turn and move towards a target at the same time…

:bust_in_silhouette: Reply From: mollusca

Try replacing

var rotTransform = t.looking_at(lookDir,Vector3(0,1,0))

with

var rotTransform = t.looking_at(target_pos,Vector3(0,1,0))

It should help with missing the target. If it’s still missing the target you’ll have to correct the course while moving, something like:

elif value == 1:
    var t = player.get_transform()
    var target_vec = target_pos - t.origin
    t = t.looking_at(target_pos, Vector3(0.0, 1.0, 0.0))
    player.set_transform(t)
    player.move(target_vec.normalized() * delta * 5)

ignore this reply

brilliantApe | 2017-07-15 23:29