- Home /
Unity2D Parallax Scrolling approach concern
I've read plenty of answers as to how to approach a moving background when making a 2D game using Unity. My question though, is more targeted to the following type of game. Currently, I want to make a game where the player does move from left to right, and at the same time make the background move. So, is this still achieved with Parallax Scrolling, where I would only move the background if the player's walk animation is played? and then if the player is in the idle animation, would the background stop? Or for this situation, is it valid to create a large static image in the background, and just have a camera following the player?
I'm not interested in how to code it, I'd just like to know what approach should be considered.
Thanks,
Answer by AlwaysSunny · Jun 11, 2015 at 10:12 PM
I'm fairly confident the effect should relate to the movement of the camera, not the player. Small difference, and maybe I'm wrong, but methinks it'll look more natural if it's determined by camera motion.
Create a quad which faces the camera. It can even be a child of the camera. Stretch the quad to fill the field-of-view. Place your background material on the quad. Write a script which changes the UV offset of the material based on camera movement. The scale of this offset delta determines the intensity of the parallax effect.
If using a single texture, either allow the texture to move so little the camera never observes a seam, or use a looping texture.
If you need multiple textures, the approach becomes considerably more complicated, and will involve multiple quads and some sneaky game design trickery known as "quilting" or "treadmilling", where you physically move a group of textured quads around based on camera motion to maintain the illusion of a single seamless background; sort of like a patchwork quilt, where only the patches that can be observed exist in a given moment.
Best luck,
I appreciate the answer, will have it in $$anonymous$$d. But I'm wondering if it's acceptable for a 2D platformer, to have parallax scrolling in the background, but at the same time have the background be large and have a camera that follows the player through the entire Background. Either that, or should I make it like an infinite runner, but stop the offset of the background if the player is not pressing right/left.
Thanks again,
I'm afraid I still do not understand the question. You'd have to show an example of the effect, or a diagram, and/or use much stronger nouns and adjectives to convey your meaning. The language in your question does not communicate anything meaningful.
Your target platform (desktop vs mobile) deter$$anonymous$$es your target size for textures. For mobile, I would not recommend any textures larger than 1024, with 512 being a decent average target. On desktop, that upper limit is 4096, and the average target is more like 1024 or 2048.
Always use the smallest texture you can while maintaining the power-of-two rule and achieving the level of fidelity you want. If you want more pixel data than these rules allow for, use multiple textures.
Regarding parallax effects, all I can say is that in every example I've seen of a parallax effect, the effect is driven by the motion of the camera. It should have nothing to do with whether the player is pressing the movement keys. It should be driven by the camera's position delta.
Does this help answer your question? If not, please reconsider the language you are using to attempt to ask it. I don't know what the phrases "background large enough...camera follows the player through the entire background" or "make it like an infinite runner" mean to you.
Ah, now I grasp what you're asking. It's always better to give more than enough information than assu$$anonymous$$g others will know how you're using a word or phrase. :)
I would not want to use the fixed-player approach. I would strongly recommend a more traditional approach. It's easier to conceptualize, program, and build for.
If your game will have dynamically generated content or you want "infinite" environments, you may face certain challenges that using the fixed-player approach would spare you, but these are challenges you'll need to get experience dealing with eventually in other genres, so you might as well face them head on. The only real benefits of the fixed-player approach are conveniences that bypass certain challenges like when and where to load / unload objects.
Hiding seams / creating a looping background texture is best accomplished in your image editing program of choice. Having multiple textures "stitched together" to form a long seamless strip will require some very careful planning and the use of the aforementioned quilting / treadmilling technique. If possible, pull elements from the background - trees, hills, mountains, into separate textures on their own quads, and animate the quads moving to simulate parallax. This is vastly superior to using a single texture or painting seamless textures for numerous reasons. These environmental "background" decorations can be spawned dynamically just like any platform objects your game will have, and can use the same system for detecting when to spawn and die.
Platforms and background decorations can die according to their distance from the player. Spawning them in an interesting way is a little more challenging, but half the fun will be co$$anonymous$$g up with a strong solution. It might be based on maintaining a certain target number of said objects, for instance, which always spawn X units away from the player, just outside the camera's field of view.
Best,
This information does help answer my question, but partially. But to clear things up, this is what I meant.
By background large enough, I meant exactly what you explained about texture sizes. So let's say I'll be including in my game multiple textures of size 1024, at different z positions.
By camera follows the player through the entire background, I meant that ins$$anonymous$$d of going the parallax scrolling route, what If I chose to actually move the player and have the camera follow it along. I've seen examples where the player is moved by changing its' Rigidbody2D, either applying forces or changing its' velocity directly, and at the same time the camera's x position changes to follow the player.
By Infinite runner, I meant going for the approach of applying parallax scrolling. Where ins$$anonymous$$d of the player's x position being changed, the player always remains stationary at (0,0), as well as the camera is stationary, and the background moves. In this case the player would always be playing a walk/run animation.
So here is my question, if my game targets the desktop platform, is it ok that I go the route of say create three textures of sizes 1024, all positioned at different z's, but without animating the textures. Simply leave them stationary. Have the user move the player through arrow keys, and as the player moves, the camera moves. This way, the player could move left to right, up, stay still, etc.. And the left corner of the Background would be the beginning of the Level, and the right corner the Ending of the level.
So is it right if I do it this way?
I apologize if I'm not being clear, I'm new to this deal of asking a questions in forums.
Trust me I appreciate your time, and I'm trying my best to be clear on this.
This morning I was reworking my answer, thinking that yesterday I hadn't posted while I was at work :D. But then when I posted my new answer this morning, I saw that what I had written yesterday had been posted as well haha. So I deleted the old one...anyways. If you see that another answer of $$anonymous$$e, similar to what you just answered to is posted, ignore it.
Thanks dude, that helps me more than enough to what approach to account for.