Help with 2D camera using _process to follow player

Godot Version

4.6.2

Question

I have a 2D topdown game where my camera used to be a child of the player (which is a rigidbody2D), but i read that it is soother if the camera is a separated and its movement is updated in _process. Unfortunately all the examples I’ve found were for 3D cameras, so I’ve tried to make due but have mostly failed.

Here is what I have so far:

extends Camera2D

@export var player: PlayerVehicle

var __target_pos: Vector2

func _ready() -> void:
	if not player:
		push_error("Combat camera wasn't assigned to player")
		return
	
	__target_pos = player.global_position


func _process(delta: float) -> void:
	__target_pos = lerp(__target_pos, player.global_position, min(delta, 1.0))
	self.global_position = __target_pos

It doesn’t need to rotate or anything, it just needs to move position.

It technically works, but it lags far behind the player and needs to play catch-up. What am I doing wrong here?

I also think I recall seeing a post awhile back saying that using lerp() in _process isn’t a great idea. Is there a better alternative?

Thanks!

Increase the interpolation weight.

min() returns the lower of two values (doc).

Using min(delta, 1.0) for the weight will likely always return delta, since it is almost guaranteed to be less than 1 (unless your game runs at less than 1 frame per second). This will be a very small amount to interpolate by.

You’ll want to multiply your weight by delta, rather than using min(), and multiply it by a larger value - 30.0 in my example below.

__target_pos = lerp(__target_pos, player.global_position, 30.0 * delta)