I looked this up and found a promising video on creating a homing missile. The problem is that this projectile will slow down or stop to reroute itself. For example, if the projectile is shot to the right, but its target is to the left, instead of the projectile turning in a wide arc at a constant speed to navigate to its target, it'll instead come to a stop and turn around in place.
I provided a .GIF of what I'm looking to accomplish. Imagine the player is the green square. The projectile is the white pixels and its target is the red X. In order for it to reach its target at a constant speed, it must turn in a wide arc to get there. I want this, but I don't know how to achieve it.
Here's the current code I have:
var current_velocity = Vector2.ZERO
var turn_radius: float = 0.15
func _ready():
current_velocity = proj_speed * Vector2.RIGHT.rotated(rotation)
func _physics_process(delta):
if get_tree().get_nodes_in_group("Enemy").size() > 0 and target != null:
dir = global_position.direction_to(target.global_position)
else:
dir = Vector2.RIGHT.rotated(rotation).normalized()
var desired_velocity = dir * proj_speed
var change = (desired_velocity - current_velocity) * turn_radius
current_velocity += change
position += current_velocity * delta
look_at(global_position + current_velocity)```
So again, this *does* work. It *does* home into the target. However, it will not do so at a constant speed, and will allow itself to slow down and stop to turn instantly. I don't want the homing projectile to slow down or be able to turn on a dime.
Playing with the `turn_radius` doesn't seem to help, because once the projectile reaches its target, it stops. I want the projectile to be able to reach its target and be able to keep traveling and then reroute back to its target, like shown in the .GIF.