Tank turret rotation inverted

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

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?

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?

fpicoral | 2019-01-20 03:24

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

kidscancode | 2019-01-20 04:02

I wrote wrong, I meant PI/2

fpicoral | 2019-01-20 04:03

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.

kidscancode | 2019-01-20 04:09

:bust_in_silhouette: Reply From: kidscancode

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()

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.

fpicoral | 2019-01-20 04:08

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.

kidscancode | 2019-01-20 04:11

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.

fpicoral | 2019-01-20 04:14

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

kidscancode | 2019-01-20 04:15

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.

kidscancode | 2019-01-20 04:18

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

fpicoral | 2019-01-20 04:18

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

fpicoral | 2019-01-20 04:21

Glad you found it!

kidscancode | 2019-01-20 04:32