Move a node2D over time

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Vityy

Hi, I’m currently working on a 2D game where there is water coming from the bottom of the screen and slowly rising to the top. I’m kind of lost when it comes to moving that water with a certain speed and how can I do to make it stay inside the camera when the player rises. It may be laughable but I can’t find any solution to my problem. Does anyone know how to achieve it? My moving water is a scene, my character and camera another one and my main game is another scene too.

:bust_in_silhouette: Reply From: Magso

To move the water you can use

water.translate(0, fill_speed)

To make it stay in the camera shot you could do something like

if water.get_global_position().y - camera.get_global_position().y => 50:
    #move camera up or slow water down.

Hey, thank you for the quick answer. I was able to move the water, for the camera I’ll try your way because I found a way that imply collisions. The problem is it’s not smooth at all. I’ll try your method. Thank you :slight_smile:

Vityy | 2019-05-07 17:03

Mmmh, I tried you way and the one with the collision seems to work better. Do you have any idea of how to smooth a translate() movement? Would be really helpful. Thanks :slight_smile:

Vityy | 2019-05-07 17:10

Translate by a float variable and add and take way as necessary to smoothen it, so something like this.

func _process(delta):
    if water.get_global_position().y - camera.get_global_position().y => 50:
        fill_speed -= 2*delta
    else:
        fill_speed += 2*delta
    fill_speed = Clamp(fill_speed, 0, 1)
    water.translate(0, fill_speed*delta)

Magso | 2019-05-07 17:18