0 votes

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

I am working with "lookat()" 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 "lookat" 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 lookat and go back to it when lookat 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:
Godot version 3.3.3
in Engine by (12 points)

1 Answer

0 votes
Node.RotationDegrees.y = LerpAngle( Node.RotationDegrees.y, Atan2( lookatdirection.x, lookatdirection.z ), 1 ) 

this rotated only the y axis.

by (32 points)

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

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

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?

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.

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!!

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.