In this video, you’ll learn how to add a
camera2D to your game and to set it up to accommodate your cinematic needs. You’ll learn about the various settings
that come with the camera, and how to effectively use them for your game. Howdy! And welcome to Game Endeavor. Cameras are an essential tool for a majority
of games. More often than not, you will have built a
vast world for your player and will need to allow them to explore it. But it is crucial that you setup your camera
correctly, because failing to do so is a quick easy way to ruin all of the hard work you
put into your game. Jerky camera movement can leave the player
feeling nauseous, or camera movement that is too slow can lag behind the player and
irritate them. Godot comes with a pre-built camera that we
can instantiate to any node. Once a camera has been parented to a node,
it will follow that node as it moves around the game. But it isn’t enabled by default. You will need to go into its inspector and
enable the “current” checkbox property before it will start rendering. This allows you to have multiple cameras in
a scene that you can switch between, which can be useful for setting up cutscenes and
action shots. There can only be one camera set as the current,
enabling current for any other camera will disable current for this camera. By default, the camera comes with a deadzone
in the middle of the screen (this deadzone is known as the drag margin), this is an area where the parent object can move within it the camera’s viewport to move with it. As mentioned before: jerky, unexpected, or
unwarranted camera movement may cause some players to feel nauseous or uneasy when they
play your game. The deadzone is a way to reduce this by minimizing
camera movement when changing directions. Especially as the player jumps up and down
or if they need to make small backtracks. This is extremely important if your game has
wall climbing in it. It is extremely unpleasant to have the camera
swaying back and forth every time the player leaps from the wall. As nice as this feature is though, it comes
with a natural flaw. You’ll notice that as the player runs, the
camera’s center is behind them. Which means the camera is showing more of
what’s behind the player than what’s in front of them. This leaves your player with less reaction
time when dodging enemies and hazards, and it looks odd for most games. In the next video, we will optimize the camera
for platformers as demonstrated here. There will be an iCard and link in the description
below. If you’re interested in platformer games
then it is a must-watch as it will drastically increase the quality of the camera. The drag margins can be adjusted in the “Drag
Margin” drop down menu. Here you will find a setting for the size
of the top, bottom, left, and right margins. You can set this to a value between 0 and
1, which represents the size of the deadzone relative to the camera’s position. It’s worth noting that the drag margin doesn’t
just apply to the parent node’s position, but also to its own global position. If in a script I change the camera’s global
position, then so long as that movement remains within the deadzone then it will not cause
the camera to move. If your camera isn’t supposed to follow
a specific character or object, such as in a top-down strategy game. But instead you’re allowing the player to
control the camera directly with either with the mouse or keys, then you will want to disable
the drag margins altogether. This can be done with the “Drag Margin”
checkboxes in the inspector. There is an option for H and V for horizontal
and vertical respectively. This will make it so that the camera’s viewport
is set precisely to its position. To prevent sudden jerky movement in your game,
you can use the smoothing that comes with cameras. This is applied automatically to the viewport
anytime the camera’s position changes. This counts for movement from the camera as
it follows its parent node, but also if you change its position in any way. There’s a checkbox to enable this in the
smoothing drop down tab. As well as a speed variable to apply to the
smoothing, which is defaulted to 5. The higher this value, the quicker and more
responsive the smoothing is. With lower values being slower and more sluggish. Finding the balance that’s right for your
game can be tricky. You don’t want it to be so fast that you
can’t notice it. But you also don’t want it to be so slow
that you can’t help but notice it. Also keep in mind that the slower this value
is, the more it will lag behind the player. This will cause an issue similar to the dead
zone that I mentioned earlier, where the camera is centered behind the player and doesn’t
show them much of what’s in front of them. Often when you’re using the camera, you
will want to limit it so that it can not go beyond your designated game world. The camera comes with built in boundaries. These boundaries are always enabled, but by
default they’re set to a ridiculous value of ten million pixels in each of the four
directions. If we go into the Editor drop down menu, then
there’s an option to draw our limits. Enable that, then change the limits to something
more local and you will see a yellow box that shows the bounds that your camera will be
limited to. The width and height of the camera’s viewport
is taken into consideration, including the zoom level. If you are setting the limits manually then
there are rulers along the top and left of the scene display window that will help you
by displaying the pixel measurements. The major ticks of the ruler are based on
the snap settings. So if you need to adjust these for your tileset,
then click the three dots in the top bar of the scene display, “Configure Snap”, and
set the “Grid Step” to whatever size you need. But if you instead want to automatically set
the limits to the bounds of your tilemap, then it is possible to get its used area. Tilemaps have the method called “get_used_rect”
which will return a Rect2 containing the upper left and bottom right corners. If you multiply this with the tilemap’s
cell_size property, then this will tell you what to set your camera’s limits to. Getting your camera just right is a major
step in polishing your game. It has a massive impact on the look and feel
of your game. So experiment with cameras to see what kind
of fun things you can do. And if you’d like to continue learning game
programming then check out this video here. And if you’re new, then join the sub-club
to get notified of future videos. And until next time, y’all take it easy.