Error: _physics_process(): Node origin and target are in the same position, look_at() failed

Godot Version

4.2.2

Question

Hello,
i have a script for my player to always look to the mouse position:

func lookAtPosition():
	var spaceState = get_world_3d().direct_space_state
	var mousePos = get_viewport().get_mouse_position()
	var camera = get_tree().root.get_camera_3d()
	var rayOrigin = camera.project_ray_origin(mousePos)
	var rayEnd = rayOrigin + camera.project_ray_normal(mousePos) * 2000
	var rayQuery = PhysicsRayQueryParameters3D.create(rayOrigin, rayEnd)
	rayQuery.collide_with_areas = true
	rayQuery.exclude = [self]
	var rayResult = spaceState.intersect_ray(rayQuery)
	
	
	if rayResult.has("position") and rayResult != null:
		var lookAtPoint: Vector3 = rayResult["position"]
		lookAtPoint.y = self.position.y
		return lookAtPoint
	return self.position

and that function is called in func _physics_process(delta) via look_at(lookAtPosition(), Vector3.UP)
And it Works!

But when the mouse does not point at the ground-plane it causes the Error-Output:

E 0:00:00:0714   player.gd:19 @ _physics_process(): Node origin and target are in the same position, look_at() failed.
  <C++-Fehler>   Condition "p_pos.is_equal_approx(p_target)" is true.
  <C++-Quelle>   scene/3d/node_3d.cpp:935 @ look_at_from_position()
  <Stacktrace>   player.gd:19 @ _physics_process()

here a screenshot from the game-scene:

and i have not the slightest clue how i can solve that error message.

I hope that someone can help me with this
Thank you :slight_smile:

p.s.
here a screenshot of Godot if you need one:

Your lookAtPosition is returning the object’s position when rayResult doesn’t have a hit. You need to detect this and avoid running the function, or return a different value.

func _physics_process(delta: float) -> void:
    var look_at_pos := lookAtPosition()
    if look_at_pos != self.position:
        look_at(look_at_pos, Vector3.UP)
1 Like

Works like a charm.
Thank you! :+1:

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