I'm trying to make a rocket that seeks out targets and turns to them, but the rotation seems to not work. My code is as follows:
var bullet_type = "Normal"
var seeking
var dead
var speed
func _process(delta):
if not dead:
position += Vector2(sin(rotation) * speed * delta, cos(rotation) * -speed * delta)
if bullet_type == "Rocket" and speed < initial_speed * 5:
speed += 30 * delta
if bullet_type == "Rocket" and seeking and not dead:
if target and is_instance_valid(target):
var target_rot = atan2(target.global_position.y - global_position.y, target.global_position.x - global_position.x)
global_rotation = target_rot
elif not is_instance_valid(target) or not target:
for asteroid in get_parent().get_node("Asteroids").get_children():
if not target:
target = asteroid
elif dist(position, asteroid.position) < dist(position, target.position):
target = asteroid
However, the rockets did not change course and simply moved in a straight line.
I confirmed that the lines of code were actually being executed by adding a print(global_rotation)
after the line global_rotation = target_rot
and it did print out the rotation in radians, but the rotation wasn't changing.
I changed the line global_rotation = target_rot
to global_rotation += 0.1
and the rotation still did not change whatsoever. This should have resulted in the rocket curving or at least changing directions, but it just moved in a straight line.
What's more is that the rockets do actually accelerate, meaning that changing the speed did work, but not the rotation.
I even tried adding a print(global_rotation)
before global_rotation += 0.1
, but it showed no difference.
Lastly, I printed the target_rot
variable, and it seemed to make geometric sense.