How to make a projectile follow a trajectory drawn with a mouse

Godot Version



Hi, I’m working on a mechanic where you can draw with a mouse a trajectory for a projectile to bypass obstacles created by enemies.

extends Line2D

var projectile = preload(“res://projectile.tscn”)
var array_pos: Array =
var count = 1

func _input(event):

if Input.is_action_pressed("path"):
	var mouse_pos = get_global_mouse_position()
	var instance = projectile.instantiate()
	instance.position = array_pos[0]

if Input.is_action_just_pressed("launch"):
	for i in array_pos:

So basically I draw a line with Line2D and then store a set of coordinates in the array, but I don’t know how to make the sprite follow them.

A rudimentary way to do this is to use the lerp() function alongside await get_tree().create_timer(length).timeout to smoothly interpolate the projectile’s position. Something like:

var target_pos = Vector2(0, 0)
for i in array_pos:
    target_pos = array_pos[i]
    await get_tree().create_timer(1.0).timeout

And somewhere else in the code; probably in _physics_process(delta):

path_to_projectile.position = lerp(path_to_projectile.position, target_pos, 10 * delta)