+1 vote

Hello guys, i'm prototyping stuff, i was trying to make a turret object rotate smoothly with the player's camera and i did it, the problem is, when you rotate the camera fast, the turret goes faster and slowly gets slower until eventually it meets its destination, but gameplay wise i don't think that would be a good idea, having to wait until it gets where it needs to. So anyway here's the code i have:

var current_deg = deg2rad(rotation_degrees.y)
var spatial_deg = deg2rad(cam.rotation_degrees.y - get_parent().rotation_degrees.y)
var new_rot = current_deg
new_rot = lerp_angle(current_deg, spatial_deg, turret_speed * delta)
rotation_degrees.y = rad2deg(new_rot)

i also have a much simpler line of code too that does the exacly same job (at least i think it does...)

rotation.y = lerp_angle(rotation.y, cam.rotation.y - get_parent().rotation.y, turret_speed * delta)

To explain what i'm doing: i'm getting the rotation of the Y axis of the "camera" Spatial Node, subtracting it's parent node Y axis rotation (this makes so the turret acts like it is a part of something and not floating along with it) and then linear interpolating the rotation of the Y axis of the turret to meet the rotation of the camera.

I really don't know if there is better ways of doing this, i spent a whole day searching for better ways of coding this, but i'm begining to think that maybe i wasn't made for programing.

All that i want to know is if there is a way to make the speed of this rotation consistent, and if not, if there's a diferrent way of achiving something alike it.

Any help will be greatly appreciated.

Godot version 3.4.1.stable
in Engine by (99 points)

Here is an example of what i'm trying to achieve: https://www.youtube.com/watch?v=MSkA6AQNM6c

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.