0 votes

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):
    print(side)
    print(zoom)

    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
Godot version 3.2.3.stable
in Engine by (56 points)

1 Answer

0 votes
Best answer

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!

by (992 points)
selected by

Thanks, this worked.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.