Hello and welcome to another episode in the Godot basics tutorial series.
In this episode we will be taking a look at the camera 2D node. The camera 2D node as a simple node that allows control of the view camera in 2D scenes. Basically it forces the screen to follow this node. Let's go ahead and take a look at the camera 2D inheritance chain camera 2D inherits from node 2D which eventually inherits from the node class. This is important because not only do we have properties and methods available to us from the camera to class we have the position property available to us from no 2D and we have access to script life cycles made available to us from the node class.
Few things to keep in mind. You can have multiple cameras inside your seen tree.
However you can only have one camera to node active at a time and if no cameras are active on your scene tree the root viewport will be shown to your player the camera to the node has the property called current. It's a property representative of a boolean value. By default it is set to false. Whenever you add camera to the node onto the same tree. However when the value of the current property is set to true that specific camera to the node becomes the active camera on the scene.
Tree on top of that. Changing the current property value to true will set all other camera 2D current property values to false.
The camera 2D comes with a method called Make underscore current it doesn't return back of value.
However when you call the make current method this method will make the current camera 2D node the active camera on the scene tree.
So in fact you have two ways of setting the current property to True you either call the make current method or you directly set the current property value to true.
One popular way of using camera 2D node is to get the camera to follow your player character when it moves and this is the basic setup to get that up and running. First you have your player character in this case it's an icon. It has a script attached to it. However keep in mind that the script being attached to the player is just a script that moves the player's position. The most important thing is attaching a camera 2D node on to the player. So keep in mind that to get the camera 2D node to follow an object in this case our player object.
It needs to be a child node of the node you wish to follow and that's because when the player moves so will all its children. Now keep in mind that just because the camera to the node is attached to the player node that is moving does not mean that our viewport is going to follow the camera. Remember the camera 2D has a property value called current and the current value is set to false by default.
That means that even though the camera 2D node is attached to our player No.
When our player node moves because current sets of folks or player will move however are you viewport will not change its perspective because by default. When all camera 2D nodes are set to false it is the root viewport that the player will see. And so what you need to do is set the current property value to true. You can do this through raw code by setting the current property value directly or you can go to the Godot app application look on to the inspector for a camera to day find the current property value and click the check mark next to on and this camera 2D node has its current property set to true.
And now when our player moves our camera will follow our player.
Another thing you may want to do is moving the camera without moving the player and to do that it's quite simple.
All you need to do is move it's inherited no 2D position property value. Now there are issues when using the camera 2D node and this is with camera lag. If you experience any camera lag use the line method to put it simply the aligned method lines the camera to that tracked
Node. Now I personally notice no issues with lag when moving objects inside of the physics process.
Virtual method however there are reports that when using Cameron's side of the process virtual method you may experience some lag. And if you do experience some lag or you have to do is call the line method provided to you by the camera to 2D node.
I uploaded a simple project file onto get hub what I uploaded to get hub is basic code. However it shows you three things.
First it shows you how to get a camera to follow a moving player. The second example is a camera moving without the player. Lastly in a different scene I have an example where there are multiple cameras attached to the scene tree and the basic code setup that gets you switching between different cameras.
Well so I have for you in this episode. Thank you so much for joining me. Thank
Thank you for clicking the Like button and thank you for clicking the Subscribe button. If you have any questions or comments please feel free to leave them in the comments section down below. I look forward to seeing you in the next episode.
Have an amazing day