Cannot interpolate camera zoom

Godot Version

v4.2.2.stable.official [15073afe3]


Hey! So, I’m kind of new to programming and i tried out lerp. I made a Hold e to zoom in system successfully, but when i tried interpolating it with lerp i didn’t work. I also checked out a few other forums before this, but i couldn’t fix the issue. Here is the script.

extends Camera3D
var weight := 1

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
	if Input.is_action_just_pressed("zoom"):
		self.fov = lerp(75, 30, weight)
	if not Input.is_action_just_pressed("zoom"):
		self.fov = lerp(30, 75, weight)

You’re close to having something that produces smooth motion.
The problem with your code is that you’re using lerp() incorrectly.

As you may know, the third parameter of the lerp() function determines the mid-point between your supplied values (30 and 75).

Your code, with weight’s actual value, looks like the following

    self.fov = lerp(75, 30, 1)
    self.fov = lerp(30, 75, 1)

Because the third parameter is always 1.0, it’s the same as writing

    self.fov = 30
    self.fov = 75

A common value for the third parameter, in this context, is delta. Try that and see what that does to your system.

Lerp with a weight of 1 will return the second passed argument.

Here’s the equation for lerp, @Sweatix covered how it reduces to a not-very-useful value for your case

func lerp(a: float, b: float, weight: float) -> float:
    return (b - a) * weight + a

Since you are using is_action_just_pressed this will also only trigger once. You probably want to use a tween.

extends Camera3D

var zoom_tween: Tween

func _input(event: InputEvent) -> void:
	if event.is_action("zoom"):
		# stop last tween if exists (spamming zoom button)
		if is_instance_valid(zoom_tween) and zoom_tween.is_valid():
		var end_fov: float = 30 if event.is_pressed() else 75
		zoom_tween = create_tween()
		zoom_tween.tween_property(self, "fov", end_fov, 0.45)
		# the last parameter, 0.45, is duration of the tween
1 Like

I get the idea now, but when i try your code, it says "Invalid call. Nonexistent function ‘set_property’ in base ‘Tween’. Weirdly enough, it also closes the game tab. When i try to go in the game again, it crashes.

because I am silly goose, it is tween_property. I will edit the example.