Am i able to do lerp with just my second position

Godot Version

4.2

Question

Im trying to make a interpolation with lerp and i have 2 positions but i want the object im trying to move only to move to one position over and over again where it can go as far back and still be able to get back to the same position with the same speed

Am I understanding correctly?

You have some object (which gets moved somehow)
Regardless of where it ends up, you then want that object to lerp back to a given position

Can you share any code you have tried? Can you share a snap shot of your scene tree for context?

1 Like

Are you using tweens?

You can tween a constant speed by getting the length to the final value and dividing it by how fast in pixels per second.

This adapted example from the tween documentation will move all children to (0, 0) at 600px/s

var tween = create_tween()
for sprite in get_children():
	var distance := sprite.position.distance_to(Vector2.ZERO)
	tween.tween_property(sprite, "position", Vector2.ZERO, distance / 600)
1 Like

The code i have for the Interpolation is

	if player.is_running == true and player.velocity.x != 0:

		t += delta * 0.2
		
		#254 starting 408 position i want to go to

		var new_pos = lerp(player.position.x, bPos.x, t)
		
		#Restricts the position to go beyond our objective
		if new_pos > bPos.x:
			new_pos = bPos.x
		
		#Applying the new position to player
		player.position.x = new_pos

And my idea was that the player runs to a certain point and if the player stops running the lerp either deactivates or the lerp allows the player to fall as far back as they need to,

but right now with my code if the player falls behind the lerp it takes them out of the locked position but always tries to rubber band them back to the destined position

Sorry about making the first question kind of vague I was really out of it

I am using Interpolation and i have not tested much with tweening, I should have been more specific with my question, is tweening able to work with characterbody2d and only affect the x axis?

1 Like

If this is part of a loop or frame like _process or _physics_process then any t value greater than zero will eventually result in bPos.x.

This is because player.position.x is always being updated, so the lowest possible value from lerp is always rising. You would need to store player.position.x as a “start value” and use that in your lerp instead of the always updating player position.


If your code does not always update or you can find a point where updates can reasonably stop, then a Tween might serve you much better.

1 Like

This part of my code is part of my _process, and ill be sure to test out what you suggested with the player position, thank you so much