What part of the script above is not working?
Looking at the script, the issue is probably how you are interpolating the movement. linear_interpolate
will give you a position that is between the input and desired location, where 0
is the starting position, 0.5
is halfway to the target position, and 1.0
is the end position.
If you are looking to move to the target position over a set amount of, then I would recommend using something like this to handle moving from the starting position to the end position:
extends Area2D
# I am assuming speed is in seconds (so 10 seconds to move to position)
var speed = 10
var current_speed_position = 0
var start_position = Vector2.ZERO
var target_position = Vector2.ZERO
func set_params(global_pos, rot, target: Vector2):
global_position = global_pos
global_rotation = rot
start_position = global_pos
target_position = target
func _physics_process(delta):
if (current_speed_position < speed):
current_speed_position += delta
if (current_speed_position > speed):
current_speed_position = speed
global_position = start_position.linear_interpolate(target_position, current_speed / speed)
This should move the object from one position to the other smoothly. It assumes the end position doesn't change half-way through, but as long as the start and end position stay the same until the object reaches the target position, I think it should work and stop exactly at the target position.
Changing the scale should be easy too, either using a Tween node or by adapting the code above to handle scale as well as position.
To adapt the code for scale as well, something like this should (I think) work:
extends Area2D
# I am assuming speed is in seconds (so 10 seconds to move to position)
var speed = 10
var current_speed_position = 0
var start_position = Vector2.ZERO
var target_position = Vector2.ZERO
var halfway_scale_size = Vector2(1.5, 1.5)
func set_params(global_pos, rot, target: Vector2):
global_position = global_pos
global_rotation = rot
start_position = global_pos
target_position = target
func _physics_process(delta):
if (current_speed_position < speed):
current_speed_position += delta
if (current_speed_position > speed):
current_speed_position = speed
var normalized_speed_time = current_speed / speed
# not 100% sure this will work, but hopefully it helps give an idea on how the scaling might be done
if (normalized_speed_time < 0.5):
scale = Vector2.ONE.linear_interpolate(halfway_scale_size, normalized_speed_time * 2.0)
else:
var tmp = normalized_speed_time - 0.5
scale = halfway_scale_size.linear_interpolate(Vector2.ONE, tmp)
global_position = start_position.linear_interpolate(target_position, normalized_speed_time)