0 votes

I wrote a script to spawn nodes for my game and this game is an endless runner so I want to move my nodes into a specific direction.

func spawn():
    var energy_scene = load("res://scenes/energy.tscn")
    randomize()
    var energy = energy_scene.instance()
    var random = Vector2(rand_range(30,230),30)
    energy.set_position(random)
    $energies.add_child(energy)

    #energy.set_position(Vector2(random.x,500)) --> but it goes directly to that location without speed, without animation
in Engine by (62 points)

1 Answer

+1 vote
Best answer

You can use a Tween

You can either add a Tween node to your scene tree, or create one manually. Either way, you can then do something like:

$Tween.interpolate_property(
    energy, // node to operate on
    "position",  // property to change
    random, // initial value
    Vector2(random.x,500), // final value
    1, // duration, in seconds
    Tween.TRANS_LINEAR, // interpolation function, see https://docs.godotengine.org/en/3.1/classes/class_tween.html#enum-tween-transitiontype
    Tween.EASE_IN, // easing function, see https://docs.godotengine.org/en/3.1/classes/class_tween.html#enum-tween-easetype
    0, delay, in seconds
)
$Tween.start()
by (1,650 points)
selected by

thanks that will do the work

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.