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.
4 comments · 67% upvoted · source · other discussions
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
dmarshallgaming [OP] 1 points · 2d ago
Good catch, thanks. That’s a real trap. Here the ships move by transform rather than physics (no interpolated rigidbodies), so the jump lands in one frame, but with interpolation on you’d see the body smear across the whole map for a fixed step. Afaik the fix is to move it through Rigidbody.position together with the transform, so the interpolation restarts from the new spot, or to turn interpolation off for that one step. How do you handle it with your 3D bodies?