Game
Dead Drift

2 years ago

CONSTRUCT TUTORIAL TIME! I wanted to make a "mini-article" on how to make the distance-based zoom control mechanic I did for Dead Drift since it's so easy!

If you're interested, click on [Read Article] below!

#indiedev #gamedev


Before we begin, a quick explanation of what's happening:

As the car distances itself from the base (the gray building at the center of the gif), the camera zooms out, to keep the base always on screen.

When the car drives back towards the base, the camera zooms in again, up to a certain point.

So -- How can we do something like this easily using Construct?

(Although this is a Construct-specific tutorial, the same principle applies to all game engines which have a camera or other similar component. If you're using another engine, you can try it!)


Layout Scale

In Construct, everything that we see on the screen happens inside a layout. A layout is like a screen or, to use jargon similar to the one used in engines such as Unity, a scene.

All layouts have a variable called Scale. The layout scale controls how small or big a layout will be shown when this scene is rendered on the screen.

image.png

When the scale is 1, the layout is drawn at a 1:1 scale. When this number is bigger than 1, the layout's pixels become bigger by that factor. For instance, if you set the scale to 2, the layout will be drawn twice as big, so it will look like you're two times closer. On the other hand, if this scale is between 0 and 1, the layout becomes "smaller" when rendered by that factor. If you set the scale to 0.25, everything on the layout will appear 4 times smaller (1/4 = 0.25).

Calculating the Scale

Since we want to zoom in or out dynamically, according to the distance between two objects, we'll need to create a Construct event.

For this event's condition, we'll want to use the Every Tick condition, which is the same to say that we'll do the event's actions every frame.

For the action, we'll use the action shown above, the System > Set layout scale action:

image.png

When writing the expression for scale value, we'll use what we've established above. The bigger the distance between two objects, the closer to zero we want this number to be.

Thankfully, there's a built-in Construct function for that. The distance function:

image.png

The distance function calculates the Euclidean distance between two points (x1, y1) and (x2, y2):

image.png

However, since we want the scale to be inversely proportional to the distance, we need to write our formula like this:

image.png

Because I want to calculate the distance between the CarBottom and the Base objects, I put their coordinates as the function's parameters.

K, on the other hand, is a Global Variable that will define how fast or slow the camera zooms out and in. If you make K = 1000, when the Car and the Base are 1000 pixels away from each other, the scale will be 1.0 (1000/1000). When they're 2000 pixels away from each other, the scale will be 0.5 (1000/2000), showing everything at half their size.

This is how the event should look like in your Event Sheet:

image.png

Min Scale

If we create the K global variable, assign it a value, and test the event we created above, we might experience an unintended side effect. If K = 1000, when the Car really close to the base, the scale can become really big. We might want to cap how big this scale gets.

One common way to control this is to say that the scale might never go over the regular 1.0 value (the 1:1 layout scale). It's simple to do that using another Construct built-in function, min. The min function simply returns the smallest of a list of values:

image.png

If we change our Set Layout Scale action to use the following expression...

image.png

...what we're saying is: "Construct, use the K/distance formula if the resulting value is smaller than 1. Otherwise, use 1."

This guarantees us that the camera will never come "too close" to the car. We can even use values other than 1, if we want the layout to be naturally zoomed out when the game is playing.


Well... that's it, folks!

If you try this, please let me know if it worked and let me see the results! I love knowing that my tutorials and tips or whatever helped you all :)

Cheers!



11 comments

Loading...

Next up

It's coming, folks!!! Anyone else excited???

QUICK UPDATE on the MISSIONS SYSTEM!

I have been working on this progression layer and things have been going well! Here's a video of me testing this system!

Click on Read Article for more info! :)

#gamedev #indiedev

Everybody talking about the Super Mario movie and here I am thinking how cool it would be if they made a movie out of THIS game...

Our 2024 Advent Calendar has opened! Day 18: @Otis_Le_PoOtis is the Creator of Bondeeā€™s Barnyard and posts updates about their dev journey and their work on the sequel!) Accept the quest and give them a follow to get Coins and a seasonal sticker!

MAKING YOUR CUSTOM BAR IN CONSTRUCT 3 - PART 2!

Time for Part 2 of our two-part tutorial on how to create your custom bar in Construct 3!

Click on [Read Article] below!

#gamedev #indiedev

Happy #WIPWednesday!

Are you working on a game?

Making some art?

Practicing a song?

Something else?

Tell us in the comments!

Fixed some nasty bugs, changed the dynamic font to Eeve Somepx's great "Hungry" font, and few things more!

Now it's time to go to sleep! See ya tomorrow!

HELLO EVERYONE! How are you doing this Monday?

Are you folks excited for this week? What are you planning to do?

I'd love to hear it in the comments!!!

@Miles_Games is a Jolter to Watch! They post great game dev updates about the holiday-themed platformer TEAM SLAY-BELLS! Follow them before the quest ends on December 24 and you'll get Coins!

Sharing the pixel art I did for a personal project I never got to make. The player would control an enchanted chest that had to investigate the disappearance of its wizard master!

What do you think? Would this had looked cool?

#pixelart