Topic was automatically imported from the old Question2Answer platform.
Asked By
Robster
Old Version
Published before Godot 3 was released.
Godot 2.1.4 Stable Linux
Hi all,
Forgetting the easing for now. How can I move a sprite from the position of one object to the position of another?
For example, I can get the position of the two locations and imagine they end up being Vector2(100,100) and Vector2(10,10).
I’ve looked at making the sprite a child of a Kinematic2D and of a RigidBody but when I play with Vectors etc I’m only pushing/moving them in the direction of the vector. I need to know how to say Move towards 10,10.
Is there a way to do that? Of course there is I just can’t find a simple answer out there is all. Can anyone lend a helping Christmas hand?
So I found the answer. It turns out it’s Tweening, for anyone in the future. Here’s something to help:
export var to = Vector2(60,100) #where we're moving to when we leave screen
export var from = Vector2(250,450) #which part of the pipeline we travel back to
export var speed = 400 #fast sprite? slow sprite?
onready var tween = get_node("Tween")
var property = "transform/pos"
func _ready():
#set our start position
set_pos(from)
moveToTarget(to, from)
func moveToTarget(end, start):
var distance = start.distance_to(end)
var time = distance / speed
if tween.is_active():
tween.stop(self, property)
tween.interpolate_property(self, property, start, end, time, Tween.TRANS_LINEAR, Tween.EASE_IN)
tween.interpolate_property(self, "transform/scale", Vector2(0,0), Vector2(1,1), .8, Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
tween.start()