look_at() in 3D - How to fix rotation on "y" axis?

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

Hello everyone! I wonder if anyone can help me with this issue. I’m fairly new with Godot and programming:

I am working with “look_at()” in a 3D game. My node can successfully look at its target, but it gets rotated with an angle respect to the floor whenever the target is above or below the floor level, and stays like that even after disabling “look_at” mode. How could I restrict the node’s rotation only on its “y” axis, so it doesn’t flip?

I have tried using Vector3.ZERO in the code below, but I get an error saying:

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

Another question is- in the case that I keep this odd rotation issue, what command could I use to save the original rotation just before activating look_at and go back to it when look_at mode is inactive again?

Thanks a lot for your help in advance!

The code I’m using is as follows:

func _physics_process(delta):

    if looking:
    		if look_target:
    			look_at(look_target.translation,Vector3.UP)



       
:bust_in_silhouette: Reply From: steffan99
Node.RotationDegrees.y = LerpAngle( Node.RotationDegrees.y, Atan2( lookatdirection.x, lookatdirection.z ), 1 ) 

this rotated only the y axis.

Actually also convert the atan2 to Degrees like this because atan2 i think returns radians instead of degrees

rad2deg( Atan2( lookatdirection.x, lookatdirection.z ) )

steffan99 | 2021-10-12 14:23

Hi! Thanks a lot for your answer!

I used the code in the following way:

var Tank = self
  
if looking:
		if look_target:
                Tank.RotationDegrees.y = lerp_angle(Tank.RotationDegrees.y, rad2deg(atan2(look_target.x, look_target.z)), 1)

But I get the following error:

“Invalid Get index “Rotation Degrees” (on base: ‘KinematicBody’ (tankscript.gd’).”

The node I’m using this script is on a KinematicBody on 3d, could it be that this type of node can’t use this method?

orumbauen | 2021-10-12 18:06

Sorry i actually gave you a c# version of the script my bad.

rotation.y = lerp_angle( rotation.y, atan2( target.x, target.z ), 1 )

I think this is the right one.

steffan99 | 2021-10-12 18:59

Hello!! Thanks! I added the code and in a certain way it worked, but the tank is looking to another side, and the camera is looking away from the target. I think it’s something that I have to fix in my nodes, I’m going to check it out and then I’ll post my advances.

Thanks a lot for your help, I learned some other things in the way as well, hehe!!

orumbauen | 2021-10-13 17:23

This didn’t work but I figured it out. Hopefully this helps someone
For anyone else using C# :

YOUR_NODE.RotationDegrees = Vector3.Up * Mathf.LerpAngle(YOUR_NODE.Rotation.y, Mathf.Atan2(OTHER_NODE.Translation.x - YOUR_NODE.Translation.x, OTHER_NODE.Translation.z - YOUR_NODE.Translation.z), 1f);

dezboyle | 2022-04-13 23:21