Rotating a thing to point at another thing

4.3.stable.official

Hi, probably a very simple answer but i just cant find it.

func _physics_process(delta):
	var next_location = global_transform.origin - nav_agent.get_next_path_position()
	

I’m trying to make my thingy move by smoothly rotating itself to face a given direction. I think the above thing gives me the target position in local space, and I need to rotate it around it’s y axis until it’s z is pointing at the target location’s x,z (not y)
Thanks in advance

I think that ‘look_at’ may be your answer.
See Godot Node3D.look_at documentation

nah, but thanks. i think i’ll have to make a new post explaining what im after in more detail

Your next_position vector is more like the direction from the target position to the thing’s position. The position is just nav_agent.get_next_path_position().

As @noufany mentioned, there is look_at, but that will snap your thing into the desired rotation, rather than smoothly moving it there. Instead, I’d go with something like:

  1. Find the direction to the target: var desired_direction = nav_agent.get_next_path_position() - global_position (the opposite of what you had in your post - we want the direction from the thing to the target.
  2. Find the angle between whatever direction your thingy is pointing in when it’s Y rotation is set to 0, and your desired_direction.
  3. Use for example lerp_angle to smoothly moved from the current angle to the desired angle
2 Likes

thanks for the help, i ended up figuring it out

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.