New to Unity... Help Creating a Parallax Background?

Hi everyone, as the title says, I’m new here, so hopefully this isn’t too difficult. I guess I can always save it for a later date if it is.
Anyways, I wanted to create a 2d game with a parallax background for my game. I also wanted a foreground that moves in front of the character. How would I go about doing this? The following are all of the questions I have about this. Again, I’m sorry that some of these things may seem obvious.

I assume the foreground needs to be a different asset than the background, but do each of the background layers need to be different as well? Do parallax backgrounds even have layers, or is it just one effect put on an image? Is a script used to give a sense of depth, is Unity itself used to create the effect, or is there some other way?

I sincerely appreciate any help. Thanks.

Most games nowadays aren’t completely 2d, one of the advantages of using 3d is that you have a reliable parallax effect for free. Hollow Knight Example.
https://unity.com/sites/default/files/styles/16_9_m_scale_width/public/article/02-6-unity_parallax.1-min-compressor.gif?itok=4QUhfOej

If you really want to stick to 2d (using the 2d renderer and pixel-perfect camera), you want to group objects together by their distance, and move them by the delta position of the camera, multiplied by a factor based on their distance (for closer objects, a bigger multiplier, so they move faster). One of the problems you might face is, since you’re moving the object based on the delta movement of the camera, if you move the camera away from the position you initially placed your objects, the movement will accumulate until you get there, and every object you meticulously positioned will be offsetted by that amount accumulated, to solve that, you want to make the parallax script always run even when not in Play Mode.
There’s a very good article in Gamasutra about this, and they provide their solution at the end, and their scripts.

1 Like

Thanks!
Is moving a project from 2d to 3d easy? Can I use the same scripts as before, and just use 2d sprites on a 2d plane as long as the z axis is lined up between the objects?

Every unity project is 3d, you just need to change your camera from ortographic to perspective. Yes you can use the exactly same scripts as before. Since your game is 2d, you might want to migrate from the 3d physics to the 2d physics, you do that changing the physics components to their respective 2d equivalents, Collider to Collider2D, Rigidbody to Rigidbody2D, etc…, but that’s not required, you can continue using the 3d physics, and keep your game the way it is.

1 Like

Ah, that’s a lot more simple than I expected. Thanks!
Also, I do already have the 2D components for my entities. Thanks anyways though.
So, to get the layered background, all I need to do is set the camera to perspective instead of orthographic and, presumably, put the layers of my background at different distances behind the scene?

1 Like

Yes, try it, place some objects closer and farther from the camera.

1 Like

I just got around to trying it (after fixing some glitches that arose from switching camera to perspective) and it looks fantastic - exactly what I was trying to achieve. Thank you so much for the help, and sorry for the delayed responses.

1 Like