To move an object to a set coordinate

It’s the same as the title.
Is there a way to move an object to a set coordinate and then stop it?
I keep looking for ways and I don’t know how to code it…
Could you help me?

Like, move it gradually towards a position, or instantly teleport it?

If you wanna move the object at a constant speed and stop at its destination, move_toward does that - it exists for floats and vectors.

If you just want the object to instantly teleport somewhere, just set its position or global_position.

1 Like

Oh!!!
Probably I need the ‘move toward’!!!
Thank you so much for your help!!!

I’m really sorry, but Could you tell me how to use this?

Example with 2D vectors - this script is meant to be attached to the node you want to move, and has an export variable that needs to be set to the target position:

@export var move_speed = 200
@export var target_position : Vector2 = Vector2.ZERO

func _process(delta):
    global_position = global_position.move_toward(target_position, move_speed * delta)

The reason I multiple the movement speed by delta is to make sure that the movement speed is the same regardless of frame rate. This means that the movement speed is in pixels/second - or, if you do this in 3D, it’d be meters/second.

1 Like

Aha!!!
I got it!!!
Thank you so so much!!!
Have a nice day😊

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.