Lerp function keeps getting stuck at the starting value

Godot Version

v4.4.stable.official

Question

I want to lerp position.z of a node3D but it keeps coming back to the starting value.
Here is the code:

extends Node3D
var neutral_z_position = 4.485
var back_z_position = 5.964
var feed_z_position = 3.746
var playng_take_back = false
@export var creature: Node3D

func _ready() -> void:
	pass

func _process(delta: float) -> void:
	position.y = creature.head.position.y
	
	print("lerp: ",lerp(neutral_z_position,back_z_position,0.3*delta))
	if Input.is_action_just_pressed("take_back") || playng_take_back:
		position.z = lerp(neutral_z_position,back_z_position,0.3*delta)
		if position.z >= back_z_position:
			playng_take_back = true
		else:
			playng_take_back = false

and here is a result of the print in the code:


Can somebody please tell me what am i doing wrong here?

Hi,

You’re interpolating from neutral_z_position to back_z_position each frame but I don’t see any instruction updating those values. Are you changing the values anywhere? If not, that would explain why your lerp is stuck, as the function result will always be the same (only a bit different due to the delta parameter that’s not exactly the same each frame).

Try lerping from the actual current position:

position.z = lerp(position.z, back_z_position, 0.3 * delta)
1 Like

I see, i didn’t realize that the first value needed to be updated. Thank you.

1 Like

You’re welcome.

The second value could also be changed and that would result in a different result. In your case, you want to change the first one, but keep in mind that not always the first value needs to be changed. All depends on the context.

1 Like

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