+1 vote

Hello and thanks for entering on my post.

Long story short im trying to make a turret that rotates to look at the mouse position, but it's rotation is limited to 90 degrees total (-45° to 45°). The code i have right now works perfectly for a static turret, the issue is that this turret is a child of something that can rotate and spin freely, and it starts "stuttering" when it faces 180°. The current code is fairly simple:

    global_rotation = lerp_angle(global_rotation, global_position.direction_to(get_global_mouse_position()).angle(), 0.2)

    rotation = clamp(rotation, -0.785398, 0.785398)

Basically the first part controls the global rotation of the turret, making it look at the mouse position smoothly.

The second part controls the turret's local rotation, telling it not to go past -45° or 45°.

The turret works perfectly even when the node it's attached to starts turning, it tries to look at the mouse position without exceeding it's limits, but as soon as the parent turns enough to have rotated the turret 180° it starts rotating wildly.

Im aware this issue is because with angles, 360 and 0 are the same thing, but numerically they are very far apart, that's why variants like "lerp_angle" exist.

Anyways, any help is greatly appreciated!

Godot version 3.3 Stable
in Engine by (309 points)

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.