Unexpected results from look_at()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By unfa

I’ve got an eyeball in my game, that I want to be looking at the player.

However for some reason the results of eyeball.look_at(player,up) are skewed.

When the player is in front of the eye - it looks fine, but when the player goes under the eye or to the side - the angles stop matching.

Do you know why this doesn’t work as expected?

:bust_in_silhouette: Reply From: Zylann

When it comes to axes and what is considered “forward”, Godot follows OpenGL convention.
look_at() makes the given spatial node look at the target using negative Z.

Your eye is not setup to look at -Z, which is why you probably added the following rotate_x(). However it seems like it’s the wrong axis, or wrong rotation. Besides, rotate_xrotates the local transform around the global X axis (confusing!).
You may want to try rotate_object_local instead.

I don’t know how you setup your eye, so I don’t know which axis to tell you to change, but I did remake myself a test scene with an eye looking like your game. I made the eye look at +X, so to make it look at -Z, it needed to rotate by -90 degrees around Y, and this worked:

_eye.look_at(_player.translation, Vector3(0,1,0))
_eye.rotate_object_local(Vector3(0, 1, 0), -PI / 2.0)

However it would be easier if you just made your eye look at -Z :wink:

Looks like the rotate_object_local() solved the problem. My function looks like this now:

func eye_track(delta):
	eyeball.look_at(look_target, Vector3(0, 1, 0))
	eyeball.rotate_object_local(Vector3(1,0,0), -PI/2)

And the eye finally seems to follow the player without these strange distortions on the sides and below.

Thank you!

unfa | 2020-01-19 12:42

This rotate_object_local example sometimes gives an invalid result. I decided to check how a default CSGCylinder is oriented on many iterations. Around 1/20 of those are incorrect. Test project on GitHub.

6r1d | 2020-06-20 15:40