How to make two lerp with different "to" values to reach it at the same time?

Godot Version

Godot 4.2.1.stable

Question

Hi!

I have two lerp functions, which operate two different parts of my character. I need them to reach their “to” value at the same time, while the “to” value is always different for both lerps.

# Here the player adjusts the swingForce and dragForce variables to a value of 0.0 to 1.0.

	if Input.is_action_just_pressed("space") && not throwingBall :
		throwingBall = true
		setSwingForce = swingForce
		setDragForce = dragForce

# Here I have a function that lerps swingForce and dragForce back to 0.0 . setSwingForce and setDragForce saves the values the player has registered.

	if throwingBall:
		swingForce = lerp(swingForce, setSwingForce, 0.1)
		dragForce = lerp(dragForce, setDragForce, 0.1)

Please let me know if anyone has a more optimal way to achieve this. Or maybe an equation to put in the “weight” argument, with the variables given.

Thanks!

Typically when you lerp between points you have A(start point), B(end point), and T(Percentage between A and B). However in your code with each call of the lerp function here you’re actually modifying the value of A. I’m assuming this is to make a cool curve where it slows down the closer it is to it’s destination :slight_smile: . The side effect of this is that the “lerping” will NOT take a specified time, and A will actually never reach the “To” value, instead getting infinitesimally close.

If I wanted to do vanilla linear interpolation between values, here’s what I’d do.

var throwingBall : bool

var startSwingForce : float
var endSwingForce : float

var startDragForce : float
var endDragForce : float

var swingForce : float
var dragForce : float

var lerpT : float # value between zero and 1
const lerpDuration : float = 5 # How long the lerp will take in seconds


func _process(delta):
	if Input.is_action_just_pressed("space") && not throwingBall:
		throwingBall = true
		lerpT = 0
		startSwingForce = swingForce
		startDragForce = dragForce

	if throwingBall:
		lerpT += delta / lerpDuration
		swingForce = lerp(startSwingForce, endSwingForce, lerpT)
		dragForce = lerp(startDragForce, endDragForce, lerpT)
		if lerpT >= 1:
			# at this point your lerp is done, swingForce == endSwingForce
			throwingBall = false

Works perfectly, many thanks @kahshmick!

I think I’m getting it, I will continue doing researches on lerps. Thanks for the explanations too!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.