How do I create a Camera Zoom Toggle?

Not favoritedFavorited Favorited 0 favourites
From the Asset Store
All popular touch mechanics - scrolling, zooming, swiping
  • I'm trying to learn to use lerp for an animated zoom for a layout, but I'm not really sure what I'm doing. I know it's lerp(A,B,X).

    "A" being the starting position.

    "B" being the end position.

    "X" being the time it takes between positions.

    Unless I'm wrong. What do I type for "A" if I want the layout scale to be "0" and "B" to be "1.5"?

    Edit: Here's the final version for Zoom Toggle so you don't have to read through he entire thread. Lerp was abandoned for better Tween solution. Many thanks to dop2000 !

    ZoomToggleFinal.c3p

    Tagged:

  • On start of layout: Set layout scale to 10
    
    On every tick: Set layout scale to lerp(layoutScale, 1, dt*4)
    

    It’s not a great method, because lerp will keep adjusting the layout scale very close to 1 for a long time. I’d use the Tween behavior instead — run a value tween and update the scale with this event:

    Object tween "zoom" playing: Set layout scale to Object.Tween.value("zoom")
    
  • Thank you for helping me. I'm an animator and not a programmer, so I don't understand what you are telling me.

    I’d use the Tween behavior instead — run a value tween and update the scale with this event:

    How do I run a "value tween?"

    > Object tween "zoom" playing: Set layout scale to Object.Tween.value("zoom")
    

    Maybe create a sprite object (call it "ZoomIn"), add a tween behavior to it, and when the sprite tween is triggered, scale the layout to it? Something like...

    Condition:

    ZoomIn | is Tween "zoom" playing

    Action:

    System | Set layout scale to ZoomIn.Tween.value("zoom")

    I tried it and it didn't work for me though.

  • Yes, you can use a special object with Tween, or just add Tween behavior to an existing object - say, to a background sprite.

    I tried it and it didn't work for me though.

    Can you post a screenshot of your code?

    Check that the "scale rate" property on layers is set to 100%

  • I appreciate you helping me with this. Here's a screenshot of the code.

    After pressing "Z" the screen goes blank. I'm guessing the layout zoomed too quickly for too long?

  • dop2000 I used the lerp example you provided me with. Works great. If I put the layout scaling event into a group and then deactivate the group shortly after activating it, does that solve the issue with the layout scaling for too long? Here's what I have to toggle Zoom:

    If the Tween behavior method is still better in your opinion, I'd like to learn how that works.

  • You’re super close, just one small correction about how lerp() works.

    lerp(A, B, X) is:

    A = current value

    B = target value

    X = how much to move toward B (usually a small number like 0–1)

    X is not time in seconds. It’s a blend factor. So if you use 0.1, it moves 10% closer each tick.

    For Layout Scale (0 → 1.5)

    You don’t manually type 0 as A every time.

    A should be the current layout scale.

    So in Construct 3, you’d do something like this:

    Every Tick

    → Set Layout Scale to:

    lerp(LayoutScale, 1.5, 0.1)

    Here:

    LayoutScale = current value (A)

    1.5 = target zoom (B)

    0.1 = smoothing speed

    That will smoothly zoom toward 1.5.

    Important mistake to avoid

    If you write:

    lerp(0, 1.5, 0.1)

    It won’t animate properly because it keeps starting from 0 every tick.

    Cleaner setup (recommended)

    Create a global variable:

    targetZoom = 1

    When you want to zoom in:

    Set targetZoom to 1.5

    Then in Every Tick:

    Set Layout Scale to lerp(LayoutScale, targetZoom, 0.1)

    Now you can zoom in and out smoothly by just changing targetZoom.

  • After pressing "Z" the screen goes blank. I'm guessing the layout zoomed too quickly for too long?

    No, the screen goes blank because the tween value is always zero. Your mistake is that you are using the wrong action - you need Tween value action, not Tween two properties.

  • robloxguy14 Thank you for that thoughtful clarification and example. I feel I understand lerp better now. You eliminated a few follow up questions I had planned.

    dop2000 Ah! I see my mistakes now. I was thinking of zero as default (no zoom), because of how zero works in other plugins. I'll spare you the explanation for how I was confusing Tween Value. It works great now! Very creative solution and may become handy if I ever need to tween other things that are unable to be given a tween behavior directly. Thank you so much!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • dop2000 (or anyone that knows) If I'm not bothering you too much, can you help explain to me why does this work

    But not this?

    It kinda works, but not as intended. I want to toggle zoom in and out. The variable changes the Tween value for the layout to scale to and it does, but without any tweening and at various scaling between 1 and 1.5. What am I doing wrong?

  • On your second screenshot, the last event (Is tween playing) is nested under the parent event (On key pressed). It needs to be a separate, top-level event.

  • Like this?

    Only Zoom Out works now (CameraZoom=1 event). Zoom In doesn't work. I can probably make it work with 2 separate camera sprites, but I'd like it to work with only one, because I plan to do some dynamic camera movements and it would just be easier with a single sprite.

  • dop2000 A bit off topic, but how should I credit you in my game (should I ever publish it) for helping me? Your real name or dop2000? Which is better? It's going to amount to absolutely nothing, nobody will probably ever see it, or play the game, except it matters to me giving credit where credit is due. Maybe I should just credit people without ever asking? Same question to robloxguy14 and igortyhon

  • Only Zoom Out works now (CameraZoom=1 event). Zoom In doesn't work.

    That's because CameraZoom can never be 0. Check your logic - first you're adding 1 to CameraZoom, then comparing it to 0. Should be the other way around.

    If you want to toggle a variable between 0 and 1, there are several easier ways to do this:

    Set var to (var=0)
    Set var to (var=0?1:0)
    Set var to (var+1)%2
    

    Or you can use a boolean variable - it has a toggle action.

    how should I credit you in my game (should I ever publish it) for helping me? Your real name or dop2000?

    This isn't necessary, but you can put dop2000 :)

  • This isn't necessary, but you can put dop2000 :)

    It is to me. I appreciate you helping me to learn something new and to think differently.

    That's because CameraZoom can never be 0. Check your logic - first you're adding 1 to CameraZoom, then comparing it to 0. Should be the other way around.

    Ok, but why can't the CameraZoom variable be zero? I'm not using it for its numerical value. I'm using it as a placeholder. I'm not multiplying or dividing different numbers that always result in 0. I'm adding only as means to change the placeholder. I've been told before I can do that and it has worked in the past...

    I did finally get the zoom toggle to work with using 0 as a placeholder and even used -1 as the other placeholder. Here's the events:

    It's clever to use a sprite tween for zooming. Couldn't had figured this out without you. Thank you!

    If you want to toggle a variable between 0 and 1, there are several easier ways to do this:

    > Set var to (var=0)
    Set var to (var=0?1:0)
    Set var to (var+1)%2
    

    Easy for you, but it seems more complicated to me. Interesting though. Help me think like you. What does var=0?1:0 mean?

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)