![]() size () position = lerp (position, p, move_speed ) # Find the zoom that will contain all targets var r = Rect2 (position, Vector2. sizeįunc _process (delta ): if !targets : return # Keep the camera centered between the targets var p = Vector2. onready var screen_size = get_viewport_rect (). In combination with parallax scrolling, it’s possible to get a very rich visual effect with very little effort.Extends Camera2D export var move_speed = 0.5 # camera position lerp speed export var zoom_speed = 0.25 # camera zoom lerp speed export var min_zoom = 1.5 # camera won't zoom closer than this export var max_zoom = 5 # camera won't zoom farther than this export var margin = Vector2 ( 400, 200 ) # include some buffer area around targets var targets = # Array of targets to be tracked. The Camera2D node in Godot is incredibly easy to use. Motion/scale is what we want to change to adjust the movement speed of the background. The properties of the ParallaxLayer are straightforward: Your scene tree should now look like this: Add one to the tree, and make the background its child. For this reason, the ParallaxBackground node needs to contain one or more ParallaxLayer nodes. This node controls parallax scrolling, but you might want to have more than one layer of scrolling background. Switch to the main scene and add a ParallaxBackground node. As you might expect, Godot has a node (actually, 2 nodes) for that. Specifically, if we have it move slower, it will give the impression that it is much farther away. This means we want to have our background scroll at a different speed from the foreground. Parallax Scrollingįinally, let’s add a really cool effect: parallax scrolling. Set the width and height ( w and h) to (3072*4, 1536) and now you will see a long repeating texture. When you check the ‘Region’ property, the background disappears! Don’t worry, we need to fill in the Region Rect property as well. So let’s adjust our background texture so Godot knows it can be tiled: What we want is for the background image to repeat itself as we scroll to the right. Now we need to fix the issue of the background only covering part of the world. Smoothing controls how the camera “catches up” to the player when it moves, and the Drag Margin settings will let you adjust how close to the edge of the window the player can get before the camera moves. That’s it! Hit play and now you have a camera locked to the player:įeel free to experiment with the other camera settings in the Inspector. It is possible to have multiple cameras in a scene, but only one can be Current, and that is the one the game window will track. In order for the camera to work, you must current the Current property. Here is what you’ll see in the Inspector: Switch to your player scene and add a Camera2D - I named mine camera. This is easily done in Godot using the Camera2D node. We need a “camera” that will track the player wherever he goes. Now we have a much bigger “level” to run around in, but we can’t get to the new platforms because they’re off the screen. Take the new instances and arrange them to the right like this: Start by going to your main scene and duplicating the platform instance 2 more times. You can watch a video version of this lesson here: We don’t believe in dumbing-down the material for kids, and game development is hard - so it will probably be challenging no matter what your age. If you’re new here, a quick note about this website: we’re called KidsCanCode because we teach programming and game development to kids, but if you’re an adult you’re welcome here, too. If you’ve never used a game engine before, or if you’re just new to Godot, this is the place to start. Godot 101 is an introduction to the Godot game engine and how it works. If you haven’t already read through the previous parts, please start with Part 1. In this installment, we add a camera to follow the player, and make a scrolling background. Godot 101 - Part 12: Camera and Scrolling Background by Chris Bradfield Sat, Tags:
0 Comments
Leave a Reply. |