0 votes

Hey there!

I'm trying to make a top-down tank game and I would like that the turret follows the mouse position. Since I'm planing on adding rotation speed, I can't use look_at(). I managed to get it working but with a litte problem: it's inverted. See the problem on this video.

The code that I'm using is this:

var turret_target = position.angle_to_point(get_global_mouse_position())
$Turret.rotation = turret_target

I already tried removing 2 * PI from turret_target and multiplying it by -1, but the result was the same.

What am I doing wrong?

in Engine by (521 points)

I think that I am an step closer to the solution: it's calculating the angle from the position, which is the same regardless the rotation. What can I do to get this working?

Note: 2 * PI is 360 degrees, so you weren't doing anything with that. Adding PI would have done what you needed.

I wrote wrong, I meant PI/2

But PI/2 would have only rotated it 90 degrees, rather than the 180 you needed. If you're not used to thinking in radians you can always use deg2rad() to get your angle values.

1 Answer

+1 vote
Best answer

It's because you're doing it backwards, finding the angle from the mouse to the position, rather than the opposite.

Instead, try this:

$Turret.rotation = get_global_mouse_position().angle_to_point(position)

Alternatively, you can find the direction vector (from tank to mouse) and get its angle:

$Turret.rotation = (get_global_mouse_position() - position).angle()
by (21,539 points)
selected by

I just realized that I'm following your tutorial hahaha. Thanks for the help! Do you know if it's not working with Godot 3.1? I started today and I have the same exact code from the Part 1 but it's not moving forward or backward, only rotating right and left. I already checked and my input map is fine and the velocity is updating normally.

I wondered about that. If you're only in Part 1, be aware the enemy tanks later will have turrets with rotation speeds - it's only the player tank that uses look_at()

There shouldn't be anything in this project that doesn't work in 3.1.

Yeah, I don't know if something has changed about move_and_slide() on 3.1 but I created a test project in 3.0.6 with the same code and it's working. I will probably go back to 3.0.6 and wait for the stable release.

I just opened the project in 3.1 and everything is working fine except some of the particle effects need adjusting.

But yes, unless you know what you're doing, I would stick with 3.0. "Beta" means there are still plenty of bugs and minor issues that still need to be fixed.

Could you try my project?
https://github.com/feRpicoral/TankDown

Nevermind, found the bug. I don't know why my collision shape was deleted. This is wy I could not move.

Glad you found it!

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.