Making camera follow the player when the player finished its move

Godot Version

v 4.7.2

Question

Hello, so i am trying to make a tile based movement and a camera that follows player in a specific way. The way it would work is the player would move one tile to the left, right, forward or backward - but only when player finished the movement, camera would catch up to the player instead of doing right after it player started moving.

I am doing it in the 3d scene and i unlinked the camera from the character-body in order to be able to code the moment when the camera starts to move. I am quite a beginner at coding and i tried using lerp, but i can’t manage to set it only for specific axis and when i do it for the general position, the camera just goes straight to the player while i need it to move only horizontally.

i am gonna paste the current code i have attached as a script to the camera, though it is not like this is the only code that i tried.

Edit - i am not sure if lerp is called a function so i removed the word

Edit - Oh right, sorry i forgot to mention it. I am trying to make a top-down game in a 3d scene so camera has to look only in one direction and move to the left, right, forward or backwards

func _physics_process(delta: float) -> void:
	if position.distance_to(player.position) > 10:
		position = lerp(position, player.position, 1.2 * delta)
		

In a situation like this I will always reach for the Tween class. After the player finishes moving I would create a Tween to animate the camera global position.

Check out the Godot docs for the Tween class.

Thanks, i guess tween did what i needed.

Try lerping only the X/Z axes and keep Y unchanged:

var target = Vector3(player.position.x, position.y, player.position.z)
position = position.lerp(target, 5.0 * delta)

Just set the target after the player finishes moving one tile. This will make the camera smoothly catch up without moving vertically.

Thanks, i didn’t try your code for now but i might choose lerp instead of tween later on. However lerp seems to function “in real time” instead of being activated like a button until it reaches the needed position, which i struggled to make it work since i am using if statements that wouldn’t allow the lerp to continue because the condition in if changes the moment it is activated i guess.

edit - improper writing correction