How to fix look_at() sending error for Vector3 up being in the same direction

4.3.Stable

Hello! I have a script that uses the look_at() function, However sometimes when the distance between the nodes position and the position its trying to look for are incredibly small it sends an error.

ObjectRef.look_at(LookAtPosition)

However i fixed it by measuring the distance between the node and not using look_at() when the distance is less than a certain amount.

if ObjectRef.global_position.distance_to(LookAtPosition) > 0.2:
     ObjectRef.look_at(LookAtPosition)

Now that’s all fine but sometimes I run into this error

Up vector and direction between node origin and target are aligned, look_at() failed.

I tried adding another if to the script but it didn’t work

if ObjectRef.global_position.distance_to(LookAtPosition) > 0.2 \
and ObjectRef.global_position.direction_to(LookAtPosition)).distance_to(Vector3.UP) > 0.5:
     ObjectRef.look_at(LookAtPosition)

This doesn’t actually affect anything but I don’t like seeing the error message, is there a fix to this?

distance of on two directions probably won’t work, you could compare them exactly or use a dot product. Make sure to use distance_squared_to if you check this often, like per-frame and/or have a constant number to compare to, it is a faster function.

var direction := ObjectRef.global_position.direction_to(LookAtPosition)
if direction.dot(Vector3.UP) != 1.0:
    ObjectRef.look_at(LookAtPosition)
var direction := ObjectRef.global_position.direction_to(LookAtPosition)
var Distance := ObjectRef.global_position.distance_squared_to(LookAtPosition)
if direction.dot(Vector3.UP) != 1.0 and Distance > 0.5:
	ObjectRef.look_at(LookAtPosition)

So I tried using your method, and it seems to reduce the amount of times it shows up but doesn’t stop it completely.

Have you tried:
ObjectRef.look_at(global_position + direction)
This worked for me.

I ran into this problem (again) today.
this time I used a different method from the error message:

if not (stl - k).cross(Vector3.UP).is_zero_approx():
	tmp.look_at_from_position(k, stl, Vector3.UP)

I just copied the method godot uses to throw the error.
This is a bad behaviour of the engine, maybe I will open an issue later or even fix it myself, It would not be the first time.
I think the engine should not be supposed to throw this error and force the user to do the check manually, since we are doing it twice.

1 Like