I have implemented a tank rotation system but wont rotate correctly

Godot Version



I am making a top-down tank game and in my code, I implemented the main gun to rotate according to the cursor, so it tracks it and moves there. But whenever I drive my tank forward (top down and goes to the top), (but works when I am going to the side),it doesn’t seem to follow the cursor and the turret is usually opposite. Here is my code,

var speed = 900
var rotation_speed = 1

func _ready():

func _physics_process(delta):
	var move_input = Input.get_axis("Forward", "Backwards")
	var rotation_direction = Input.get_axis("Turn Left", "Turn Right")
	var adjusted_speed = speed
	if move_input > 0:
		adjusted_speed = speed * 0.5
	velocity = transform.x * move_input * adjusted_speed
	rotation += rotation_direction * rotation_speed * delta
	# Retrieve cursor position
	var cursor_position = get_global_mouse_position()
	# Get direction/rotation from tank to cursor
	var turret_rotation = global_position.angle_to_point(cursor_position)
	# Assign the turret rotation (but correct it by the body rotation)
	$Turret.rotation = turret_rotation

i think you would have to subtract the tank.rotation from the turret rotation since its the parent its rotation is added to the turret rotation

1 Like