How to stop moving the tween

4.5

How do I make my tween bob up and down on its position? Because it keeps going to the coordinate “1.0” , I want it to move on where it is, not move to 1.0.

func _ready() -> void:
	bobbing_animation()


func _on_area_entered(area: Area2D) -> void:
	if area.is_in_group("player"):
		queue_free()

func bobbing_animation():
	tween = create_tween().set_loops()
	tween.set_ease(Tween.EASE_IN_OUT)
	
	tween.tween_property(sprite, "position", Vector2(1.0, 4.0), 0.3)
	tween.tween_property(sprite, "position", Vector2(1.0, 0.0), 0.

(Yes I know I used the area_entered trigger wrong.)

What is sprite?
If you only want to animate the y-position, then do that with position:y instead of position.

1 Like

Sprite is the coin sprite.

And I tried your “position:y” and it gave me an error.

What error?
I assume sprite is defined?

1 Like

Yes, the sprite is an @onready variable for my Sprite2D.

The error says: coin.gd:17 @ bobbing_animation(): Type mismatch between initial and final value: float and Vector2

I dont know why but something’s wrong with your position:y thing.

position:y is only one value (a float), so you must supply it only one float value, not a Vector2

tween.tween_property(sprite, "position:y", 4.0, 0.3)
3 Likes

Oh wow, thank you everyone… i’m not used to tweening so I didn’t know these stuff. :sweat_smile:

Hold on, I have a new issue.

I can’t put my coins high or low anymore because it’s gonna go to a specific y level.

extends Sprite2D

@onready var sfx: AudioStreamPlayer2D = $Collect
@onready var sprite: Sprite2D = $"."
@onready var tween: Tween = null

func _ready() -> void:
	bobbing_animation()

#read this one
func _on_area_2d_body_entered(body: Node2D) -> void:
	if body.is_in_group("player"):
		sfx.play()
		body.score += 5
		tween = create_tween()
		tween.tween_property(self, "position:y", -10.0, 0.2)
		tween.tween_callback(queue_free)

func bobbing_animation():
	tween = create_tween().set_loops()
	tween.set_ease(Tween.EASE_IN)
	tween.set_trans(Tween.TRANS_SINE)
	
	tween.tween_property(sprite, "position:y", 6.0, 0.5)
	tween.tween_property(sprite, "position:y", 0.0, 0.6)

You will likely have to change your scene tree to add a Node2D as a parent of your sprite, you can place the Node2D anywhere and the sprite’s position will change relative to it as a child.

1 Like

Thank you very much.

I think that’s all my coin problems👍

The beautiful thing about Tweens vs AnmationPlayer is, that you can actually do relative animation with them.
Try this: Instead of
tween.tween_property(self, "position:y", -10.0, 0.2)
do (in your if statement):

var current_position: Vector2 = position
(...)
tween.tween_property(self, "position:y", current_position.y - 10.0, 0.2)
3 Likes

That’s so cool.

I’ll do that if my code is destroyed👍

Or even the .as_relative() method

tween.tween_property(self, "position:y", -10.0, 0.2).as_relative()
3 Likes

Oh, this is cool! There are so many things to be missed in going through the documentation :smiley:

1 Like