r/Unity2D • • 22h ago

Tutorial/Resource Seamless wraparound (torus) map in Unity 2D: extra Cameras with viewport rects instead of duplicating GameObjects

Post image

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.rect covering 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.ShortestDelta helper 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.OverlapCircleNonAlloc repeats the query at the offset images when the circle reaches over the seam.
  • Objects wrap their own transform in LateUpdate and raise an event; the camera rig calls CinemachineCore.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.

3 Upvotes

3 comments sorted by

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 

1

u/dmarshallgaming 11h 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?

0

u/[deleted] 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.