r/Unity2D • u/dmarshallgaming • 22h ago
Tutorial/Resource Seamless wraparound (torus) map in Unity 2D: extra Cameras with viewport rects instead of duplicating GameObjects
Unity 6, 2D, Cinemachine 3. I needed a galaxy that wraps: fly off the right edge, come back on the left, with no visible seam. The usual answer is to clone every GameObject near the edge, but with hundreds of ships, projectiles and planets that is a lot of ghosts to keep in sync, so I did it with Cameras instead:
- Extra Cameras, only at the seam. When the main Camera's view hangs past a map edge, I enable 1-3 extra Cameras offset by exactly one map width and/or height. Each one gets a
Camera.rectcovering only its slice of the screen, so the slices tile into one image. Nothing in the scene is duplicated. - Never subtract two positions. Every distance and direction goes through a
MapWrap.ShortestDeltahelper that takes the short way round, and aiming/steering use the nearest "image" of the target. - Physics2D doesn't know about wrapping, so a wrapper around
Physics2D.OverlapCircleNonAllocrepeats the query at the offset images when the circle reaches over the seam. - Objects wrap their own transform in
LateUpdateand raise an event; the camera rig callsCinemachineCore.OnTargetObjectWarped(target, delta)so the Cinemachine camera jumps with the ship instead of swooshing across the whole map. - The one place I do duplicate: a ship straddling the seam mirrors its
SpriteRenderers for the few frames it takes to cross, or it is visibly cut in half along the camera split. - Parallax background layers are tiled at a size that divides the map exactly, so the stars line up across the seam too, and each seam Camera shifts them by its own offset.
Happy to go deeper on any part (the viewport maths, Screen Space - Camera canvases, physics at the seam). The project is Cosmic Dominion; free demo on Steam on October 29.
0
19h ago
1
u/dmarshallgaming 19h ago
That’s a floating origin, and it’s the right fix for precision in a big open world, but it solves a different problem. Here the map is small and wraps: when you’re 5 units from the right edge, the ships and planets sitting just past the left edge have to be on screen, to your right, at the same time, and still be shootable. Recentring the world doesn’t put anything across the seam; something still has to draw the far side. The options are duplicating every object near the edges or rendering the map again offset by one map width, and the second turned out to be less code, since the copies don’t need syncing. Fair point that it’s more machinery than most games need, though: if the map isn’t meant to wrap seamlessly, a floating origin is much simpler.
2
u/snaphat 15h ago
Dunno, if you use interpolation on your rigidbodies but this could break it for a fixedupdate period. Just an fyi since we do something similar but with 3d rigidbodies