I have a problem with a Camera2D and the zoom value

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By antoniodev

I am trying to do a loop where the camera zooms in and then zooms out, but I have no idea what am I doing wrong, if someone can please help me with the code it will help out a lot, thanks.

extends Camera2D

var side = true

func _process(_delta):

    zoom.y = zoom.x

    if side == true:
	    zoom.x = lerp(zoom.x, 0.55,0.1)
	    if zoom.x == 0.55:
		    side = false
    elif side == false:
	    zoom.x = lerp(zoom.x, 0.6,0.1)
	    if zoom.x == 0.6:
		    side = true
:bust_in_silhouette: Reply From: Yuminous

But what’s the actual problem you’re having?
I’m assuming it doesn’t work at all? Or…?

Anyway, your lerp doesn’t allow the if zoom.x == 0.55 to run because:
lerp is a math equation which –depending on your parameters– may only bring the value near the mark (never actually reaching it).

So in this case if zoom.x == 0.55 is never true because for example
zoom.x = 0.549999999999…

You could solve this by including a stepify (rounding) function, like this:

if side == true:
	zoom.x = lerp(zoom.x, 0.55, 0.1)
	var i = stepify(zoom.x, 0.01)
	if i == 0.55:
		side = false
elif side == false:
	zoom.x = lerp(zoom.x, 0.6, 0.1)
	var i = stepify(zoom.x, 0.01)
	if i == 0.6:
		side = true

Second up: your zoom.x values are changing by 0.1 = 10% every physics frame — AKA 60 times a second (by default) which will probably change too fast to notice and definitely too fast to be comfortable.

Set the lerp % value from 0.1 to something like 0.01 to slow it down.

Finally: I don’t know how big Camera2D zoom values are meant to be, but 0.55 – 0.6 looks like a really small change. Is it big enough to even be noticeable?

Please add more details to your question if that doesn’t help your situation.
But good luck anyway!

Thanks, this worked.

antoniodev | 2021-07-15 08:51