Tweening not going as expected

Expectation:

The tween is supposed to move the object between two positions.

These positions are below and above the movable object.

The object is supposed to tween up, then tween down to the second position, and repeat.

Issue one:
Only tweens to the first position without tween.kill().

First position

tween.tween_property(self,“position”,Point_1.get_position(),0.5).set_delay(0.2)

When I try to kill the tween normally, it doesn’t play (because the tween never started, I’ve tried putting Tween.kill() under the property before, it still just stops it from playing entirely)

Issue two:

When killing the tween via player collision, instead of following the player like it’s supposed to

the second tween starts, and never transitions back the first property.

Second position

tween.tween_property(self,“position”,Point_2.get_position(),1.5)

The overall issue is that the tween doesn’t switch between property methods

Which in return leads to the object not following the player.

My bad for making it sound confusing, I rushed while writing it the first time.

CODE BELOW:

extends RigidBody2D

var float_up :bool = false
var float_down: bool = false
var is_in_grab_zone: bool = false
@export var Point_1: Node
@export var Point_2: Node
@onready var Up_T = $"Up_timer"
@onready var Down_T = $"Down_timer"
@onready var sling = get_node("../../Sling")


func _ready():
	pass


# Called every frame. 'delta' is the elapsed time since the previous frame.


	

		

func _physics_process(_delta):
	var tween = get_tree().create_tween().set_loops() 
	if is_in_grab_zone == true:
		tween.kill()
		self.position += sling.position - position

	if tween.finished:
		tween.kill()
	tween.tween_property(self,"position",Point_1.get_position(),0.5).set_delay(0.2)
	
	tween.tween_property(self,"position",Point_2.get_position(),1.5)
1 Like

I’m trying to understand the problem, but your explanation is confusing me. Please reword your question in simple terms: What’s your expectation. What’s happening.

Also, please, use CTRL-E (preformatted text) to put your code in as without that, it makes things even harder to follow.

I don’t get what you’re trying to do in the first place, but your code is full of red flags:

  • You’re using a RigidBody2D, which is supposed to be controlled by the physics engine, not directly (by setting the position property)
  • You create a new tween every frame
  • Those tweens will all loop infinitely (since you didn’t provide an argument to set_loops), so have to be manually killed
  • The two cases in your code where that might happen will both never be reached (since the value of is_in_grab_zone doesn’t change and the tweens will never emit the finished signal because - again - they’re looping infinitely)
  • The line self.position += sling.position - position is equivalent to position = sling.position
3 Likes

I’m trying to give a enemy drop a floating affect, which is why I have the two points, and the tween.kill( ) method when the object is in the grab zone.

if is_in_grab_zone == true:
		tween.kill()

I picked the ridged body node because I was planning to use the built in physics features that came with it. (I should’ve done more research on the node before-hand )

I apologize for confusing you and dharhix.

I’m relatively new to coding if you couldn’t tell already (which I understand is an excuse, I take full responsibility for my awful description of the issue. )

I’ll try to use the information you gave me to adjust the code so it can work properly.

Thank you for the criticism.

In that case, move the tween setup from _physics_process to _ready, so it’s only done once:

func _ready():
    var tween = get_tree().create_tween.set_loops()
    tween.tween_property(self, "position", Point_1.get_position(), 0.5). set_delay(0.2)
    tween.tween_property(self, "position", Point_2.get_position(), 1.5)

However, do note that the change to Point_1 will be three times quicker than the change to Point_2 because you’re using different durations here! Also, the delay you apply means there will a short pause before the tween starts/repeats.

Assuming you change it’s value from another script eventually, you could keep the check for the value of is_in_grab_zone in your _physics_process. However, note that this means you’re checking the value 60 times per second! So you’re very likely better off calling tween.kill() directly from that other script.

1 Like

Thankyou for your help, it’s working as expected now. I’m gonna take a course to try and get the basic’s down packed, again, appreciate the help.

1 Like