so i am making a game where i would like one of the following to happen:
infinite scroll in both left right direction
a fixed piece of land, i.e. level made in editor, and have the player/camera loop around to the start once they reach the other end
I have my scene with 3 background and 2 foreground layers of parallax. each ‘layer’ is an empty gameObject with the graphics in for the various layers. In script my character moves left right, the camera moves with the player, and code moves the background layer containers left/right as a proportion of the camera movement to create parallax.
does anyone have any thoughts on the best way to do this? i.e what is the best approach to take?
You can loop around using 3 cameras. Have a main camera at the scroll position, then add two other cameras as children. The 2 extra cameras should not clear any background/depth at all. You need to position the extra 2 cameras so that when you reach the right edge of environment the rightmost camera starts showing the leftmost edge of the environment, and the reverse for the left edge. Then you can scroll the main camera to any position and the other 2 cameras will automatically fill in the missing bits from the ends where the land wraps around. Then you just need to make a simple script to position the main camera to scroll it, and when its scroll position gets to the end of the landscape you wrap around the scroll offset so that it stays within the width of the terrain.
The parallax could work the same. An alternative is to just repeat the terrain 3 times horizontally and use a single camera.
If you plan to use collision detection or physics it can be hard to make it work in a wraparound world, though… since objects straddling the right edge of the landscape need to be able to collide with objects straddling the left edge, which are far apart. You can do it by duplication of physics colliders, for example, but it’s not exactly intuitive.
I know this thread may be old, but I would love it if you could elaborate on this more. I want the same effect seen in New Super Mario Bros multiplayer for the DS ( https://www.youtube.com/watch?v=NMJuXOM-s1c
).
How would you go about seamlessly transitioning between cameras?
1:41 - 1:45
Look at the bottom right corner to see that Mario’s head wraps back to the front of the bar.
The reason I didn’t specify time is because the effect is continuous throughout the whole video and not able to be pinpointed exactly when the looping is apparent (hence why I want to accomplish it - because you can’t tell)
They’re not doing anything special there. It’s not a full wrap. All they do is when the X position is >0 a certain amount they subtract the width of the bar from the coordinates. Nothing to do with camera manipulation.
The bar is a smaller representation of the 2d level in which the players are interacting. I pointed out that specific time frame and the bar in the corner to pinpoint when the entire level loops (according to the in-game UI). The level as a whole is what I want to loop - all of the objects, characters, backgrounds, platforms, etc…
If a player wanted to, they could run to the right indefinitely and keep seeing the same level (not a copy) repeated over and over again.
Basically what I did was set up additional cameras. Have the main camera, and then have a second camera which is 1 whole level’s width to the LEFT of the main camera (for when you are scrolling to the right like in this video). When the main camera gets near the right hand edge of the game level, the left camera will start to approach the left hand side of the game level which will show up on the right hand side of the screen, filling in the gap.
As far as object interaction though, that’s where it becomes difficult because if you want to use physics at all, physics is not designed to wrap around whatsoever so e.g. say you have an object which is overlapping the rightmost edge of the game level and part of it is poking out into the leftmost edge of the game level (or should be). In order for physics colliders to interact on the right hand side of the level, the object/colliders have to be there, which means they can’t be on the left edge interacting with objects that are closer to the left side of the level. You’d have to duplicate the object somehow and copy all of the physics state from one object to another. And what if objects in both locations interact with the object at the same time? It becomes pretty difficult to handle. It becomes easier if you don’t use real physics and do all the movement coding yourself because then you can put this into your script and check for proximity/overlap between objects on both sides of the playing area.
I am not sure why you would need multiple cameras, just have 2 sets of backgrounds and move them. When the camera passes the mid point of one background move the other to the corresponding side.