Making npc look where it is walking

Godot Version

4.6

Question

so, I have a node that moves around using the following snippit of code:


func walkto(pos):
  while self.global_position != pos:
    self.global_position = self.global_position.move_toward(pos,deltaa*walkspeed)

idk if this code is good or not, I couldn’t find anything better.

I want the node to look towards the position it is walking but look_at() is snapping to the angle instead of rotating

and yes, I know look_at() is meant to snap but idk any other ways to look at a position nor can I find one

Thanks!

1 Like

look_at() cannot look in a “wrong” direction. It always points node’s -z axis towards the target position given in global space. Make sure that your target position is correct and that the visual representation in your model (head mesh etc…) is aiming in the direction of the local -z axis.

2 Likes

that worked! the model was rotated backwards so I fixed that.

But look_at() still is snapping, do you know a way to get it to rotate smoothly while it’s moving?

Take head’s current global transform, and call looking_at() on it to get the looking transform. Each frame slerp() head’s global basis towards the basis in the looking transform, for a small amount scaled by delta time.

1 Like

To be sure. When you describe look_at are talking about the Character3D? If you want to move in one direction is ok but if you want your model head look at a position, you should use a LookAtModifier3D

with some debugging that worked! thanks :smiley:

1 Like