0 votes

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...

in Engine by (33 points)

1 Answer

+1 vote
Best answer

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)
by (1,562 points)
selected by

ignore this reply

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.