+2 votes

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?

in Engine by (199 points)

1 Answer

+3 votes
Best answer

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 ;)

by (27,956 points)
selected by

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!

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.