my turret is not shooting at the player

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

my tower follows the player when he enter in the area of the collision shape of the tower and the tower shoot wnen the ray cast 2D collide with the player, but the bullets are not going in the direction of the player, the bullets are instead going to another direction for some reason.

there is all the script

script
extends StaticBody2D

var max_distance = 800
var pre_bullet = preload(“res://enemies/torrent_bullet.tscn”)
var rot_vel = PI * .7

var bodies =

func _process(delta):
if bodies.size():
var angle = $cannon.get_angle_to(bodies[0].global_position)
if abs(angle) > .1:
$cannon.rotation += rot_vel * delta * sign(angle)
if $cannon/sight.is_colliding():
if $cannon/sight.get_collider() != bodies[0]:
var oldbody = bodies[0]
var new_body_index = bodies.find($cannon/sight.get_collider())
bodies[0] = $cannon/sight.get_collider()
bodies[new_body_index] = oldbody

		
		

func _on_censor_body_entered( body ):
if !bodies.size():
$shoot_timer.start()
bodies.append(body)
$cannon/sight.enabled = true

func _on_censor_body_exited( body ):
var index =bodies.find(body)
if index >= 0:
bodies.remove(index)
if !bodies.size():
$cannon/sight.enabled = false
$shoot_timer.stop()

func _on_Timer_timeout():
if $cannon/sight.is_colliding():
var bullet = pre_bullet.instance()
bullet.global_position = global_position
bullet.dir = Vector2(cos($cannon.rotation), sin($cannon.rotation))
get_parent().add_child(bullet)

bullet script
extends Area2D

var max_distance = 250
var vel = 500
var dir = Vector2 (0 , -1) setget set_dir
onready var init_pos = global_position

func _ready():
pass

func _process(delta):
translate(dir * vel * delta)
if global_position.distance_to(init_pos) > max_distance:
queue_free()

func set_dir(val):
dir = val
rotation = val.angle()

:bust_in_silhouette: Reply From: exuin

I don’t really want to try to figure out all of that badly-formatted code, but I’m guessing that this issue is because the rotation of the bullet is added to the rotation of its parent, the turret. You don’t need to set the rotation of the bullet, it will be the same as the turret when it’s added as a child.