I am making space arcade with procedurally generated background consisted of nebulas and stars sprites, which spawn on Awake() in certain borders around player.

Mobile phones have limited capacity so I want to redistribute sprites along the player's movement. E.G. if player reaches an empty area where is no sprites, I want sprites which farthest relatively to player to move in front of player. So player couldn't ever see an empty area. Notice that player can move in any direction in 2D space, not only up.

enter image description here

  • \$\begingroup\$ If the player flies right far enough that all the sprites in their starting area reposition, then flies back left the same distance to get back to their starting point, do you need to rearrange the sprites to put them back exactly where they were for consistency? Or is it OK if the player can't step into the same river twice, as it were? \$\endgroup\$
    – DMGregory
    Apr 5, 2018 at 13:34
  • \$\begingroup\$ It is better if the player can't step into the same river twice) \$\endgroup\$ Apr 6, 2018 at 7:43

1 Answer 1

  1. Add a BoxCollider2D to your nebula sprites which is as large as the sprite.
  2. Add a Rigidbody2D with the Body Type "Kinematic" to your nebula sprites.
  3. Create an invisible game object centered on the camera.
  4. Add a BoxCollider2D to the invisible object which is as large as the screen and has the "Is Trigger?" property activated.
  5. Add a new script to the invisible game object which implements the OnTriggerExit(Collider other) event. In that script, check if other is a background sprite, and if so move it to the opposite side of the screen by assigning a new vector to its transform.position. When your world origin (world-space coordinates 0:0:0) is always in the center of the screen, then you can simply flip it on all axis' with transform.position = -transform.position;. When your camera is moving, the math gets a bit more complicated. How complicated depends on your actual scene setup.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .