Why is look_at causing huge amounts of lag in my 3d game?

Godot Version

4.1.3

Question

I am using the following line of code to rotate my boss to face my character the boss uses a nav region.

$FaceDirection.look_at(player_position, Vector3.UP)
rotate_y(deg_to_rad($FaceDirection.rotation.y * turn_speed))

if I have the look_at line in my code the game just CHUGS and lags a ton, any reason why. I want my boss to face my character when he is navigating, but this is making it impossible

The look_at() function by itself has barely a performance cost. Navigation is also irrelevant in this because navigation has no use for rotation.

Look if you have errors because when look_at() fails (e.g. because both positions are the same) it prints errors and having such error spam tanks performance.

If this is not the case what is most likely happening is that this rotation change triggers a lot of transform hierarchy changes to other child nodes and objects in your setup. Some of them might have very expensive updates (e.g. physics triangle collision shapes, mesh rendering with skeletons). If on top you call that rotation change up to 8 times in the physics loop your performance is already dead on paper.

It is in general better to keep your “root” node of actors without a transform rotation and only rotate the visuals or relevant parts like non-circle physics shapes. If you rotate everything you update everything with a transform that might not even use a rotation.

1 Like

Thank you so I was rotating the object with rotate_y and the script is in the root node which is a character 3, so you were right it was rotating EVERYTHING under it, so I just did a on ready and specified the thing to rotate as the character mesh only, and did a character.rotation.y = $FaceDirection.rotation.y and now I have no lag, thank you!

Thank you! I am going to try some of your suggestions now! I really want to get to the bottom of this. I think you might be right because it lagged when I calculated the rotation manually and transformed, so your explanation makes a lot of sense. I will try this and post results. Anything else you or anyone else can suggest?