Search Unity

Question Spawning objects with fixed position in an infinite world

Discussion in 'Scripting' started by kyrieortu, Mar 26, 2023.

  1. kyrieortu

    kyrieortu

    Joined:
    Feb 7, 2023
    Posts:
    8
    I have just built an infinite world, which works like an infinite side scroller, but in all dimension (it's a 2D top down shooter type of game). If the player goes far enough the x-axis or y-axis, the background will move with the player, creating an infinitely long world.

    The reason:
    You can think of the game like a typical top down chicken invaders type game, but "open world".
    Because I can't just instanciate a million objects to shoot at and collect, I decided to have a spawn area around the player that's small enough to have a pool of objects who are getting respawned in that area once they have been "destroyed" or "collected", so that the cpu does't die, but big enough that the player won't notice.

    The problem is that eventually I want to add objects with a fixed position. Think a black hole thats not moving, or moving veeeery slowly.
    So if the player goes far enough that the black hole will leave the spawn circle, it will be getting deactivated from that spot (and possibly respawn somewhere else? Depends on my future code). If the player decides to pay that black hole a visit now and tries to navigate back in that direction.. that black hole won't be there anymore.

    I have some ideas in mind, but they sound to be too complicated and it feels like there should simpler solutions.
    Thats why I'm asking here.. maybe some of you have better Ideas than me.

    Until then.. I will do some world building somwhere else for now.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,063
    You could cache the offset after spawning. This won't work at 'infinite' distances. Using a double already helps a ton