Vector(x, y) and move_toward not making an oj=bject move towards a specific spot

Godot Version

4.6

Question

hello :smiley:

Im sure this is a silly question, but ive been having a pretty big issue witha minigame I was building. I wanted to make it so that if an area was entered by a draggable object, it would run this:

extends StaticBody2D

var dragging = false
var of = Vector2(0,0)
@export var move_speed = 200
@export var target_position : Vector2 = Vector2(-148, 118)

#non-problem code cut out here

func _on_head_area_entered(area: Area3D) -> void:
	global_position = global_position.move_toward(target_position, move_speed)

And have it so that if you put it in a right combo, it would take the object and place it in a set place (target_position). But when i run it and drag the object into the space its connected to, it doesnt go towards the space? What did i do wrong?

You misinterpreted what Vector2::move_toward() actually is. It’s not going to magically animate your global_position to the target_position, as you probably hope for.

What you’re looking for is a Tween and this is an example of how you can use it in your case (this is assuming the signal works and is properly triggered) :

func _on_head_area_entered(area: Area3D) -> void:
	var tween := create_tween()
	tween.tween_property(self, "global_position", target_position, 1.0)

Check out this cool guide from @qaqelol to learn more about Tweens:

2 Likes

Hii ! Thanks for the mention!!
A tween would indeed be perfect in this situation!

2 Likes