

Otherwise, it will (predictably enough) not work. Remember to enable the “Is Looping” property of the “ScrollingScript” for the 0 - Background in the “Inspector” pane. Indeed, the backgroundPart is the exact representation of what is happening in the scene. Finally, we put it at the last position of backgroundPart list. When it’s the case, we change its position to be after the last (rightmost) child. We test if it’s completely outside the camera field. In the Update() method, if the isLooping flag is set to true, we retrieve the first child stored in the backgroundPart list.Thanks to a bit of LINQ, we order them by their X position and put the leftmost at the first position of the array. In the Start() method, we set the backgroundPart list with the children that have a renderer.We also have to use a private variable to store the layer children.We need a public variable to turn on the “looping” mode in the “Inspector” view.(The numbers in the comments refer to the explanations below) Explanations Using UnityEngine /// /// Parallax scrolling script that should be assigned to a layer /// public class ScrollingScript : MonoBehaviour You read right: without doing anything, you already have a level editor. The nice idea here is that you can use the Unity editor to set the enemies. By default, they are static and invincibles until the camera reaches and activates them. Here is what we will do: We position the Poulpies on the scene directly (by dragging the Prefab onto the scene). You could define events that spawn enemies when they are triggered, spawn points, pre-determined positions, etc. How do we spawn enemies? It depends on the game, definitely. However, we want them to wait and be invincible until they spawn.

Currently, they are just moving and shooting as soon as the game starts. Spawning enemiesĪdding a scrolling to our game has consequences, especially concerning enemies. Super Mario World has probably one the best camera possible for a platformer. Even in a platformer, the camera isn’t strictly linked to the player: it follows him under some restrictions. We would also recommend to always keep the camera independent in a 2D game. We DO want to keep the player inside a restricted area.
Parallax effect unity 2d free#
If the camera moves along with the player for both horizontal and vertical axis, then the player is free to go where he wants. In a shmup, the camera restricts the player movement. It could be a solution, but this would not fit with our gameplay. So if the camera is a child of the player and is centered on him, it will stay that way and will follow him exactly. Indeed, in Unity, if you set an object (camera or not) as a sub-child of a game object, this object will maintain its relative position to its parent. Note: you may ask: “Why don’t we just set the camera as a child of the player object?”. Thus, they are behind and seems to move slower.īut in a standard 2D game in Unity, we use an Orthographic camera. The parallax is obvious: background elements have a higher depth. The first choice is a no-brainer if you have a Perspective camera. Second choice: The player and the camera are static.First choice: The player and the camera move.Theory: defining the scrolling in our gameĪdding a scrolling axis need a bit of thinking on how we will make the game with this new aspect.

Moreover, many shmups use a scrolling in one - or more - axis (except the original one, Space Invaders). If done correctly, this gives an illusion of depth. To make it short, the idea is to move the background layers at different speeds (i.e., the farther the layer is, the slower it moves). Time to enhance our background and scene.Īn effect that you find in every single 2D game for 15 years is “ parallax scrolling”. For the moment, we have created a static scene with a player and some enemies.
