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
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)
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?
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.