Search Unity

creating tiles ahead of player game object.

Discussion in 'Scripting' started by larswik, Jan 24, 2019.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    I am starting some early tests of a top down space ship game. I am trying to solve some coding issues about generating tiles.

    In the photo you can see the camera on top and centered on the spaceship. The stars are created by a repeatable tile where there are 4 horizontal and 3 vertical for a total of 12 tiles, or Quad's with a repeatable texture map on them. The green nebula is a different tile under the star tiles and is not used to repeat, so ignore.

    As the ship move forward, backwards or what ever direction I am trying to figure out the best way of creating new tiles in the direction it is moving, while removing tiles behind it, or in the direction it is not moving.

    currently it will start with this number of tiles around it.

    Any ideas are much appreciated.
     

    Attached Files:

  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    If the tiles are seamless, then it should be as simple as taking the row furthest down (from the cameras view) and moving them up to the top. You might have to add an extra row, then just reposition the gameobjects as your ship is moving forwards.
     
  3. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Thanks for the reply.

    That solves part of the problem. I was originally thinking destroy them and instantiate new ones. Repositioning them should be faster, thanks.

    But the thing that has me scratching my head is how to detect the tiles he is moving away from to reposition them ahead of him. The problem is the ship can move in a 360 degree direction. So it could be moving the bottom row to the top row as he heads up. Then move the left side to the right as he heads right. But if he heads diagonally, say, up and right, I would need to shift the left and bottom tiles.

    So trying to figure out how/when to trigger the movement of tiles has me scratching my head.
     
  4. mziolko

    mziolko

    Joined:
    Oct 24, 2018
    Posts:
    4
    I am quite new to unity so it may be completely rubbish what I say but can't you use collider? If you have a tile above which you have spaceship and around this tile you have other tiles when you enter the collider from other tile you move other tiles so the one you entered is always in the middle
     
  5. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Hi, I have been thinking colliders too, but I need to research them more. If I can test to see if a tile collider and a large collider around the ship are no longer intersecting, then that tile can be relocated in the direction the ship is heading. This gives me some ideas to move forward on. Thanks.
     
  6. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    I'd keep a collection of the tiles, either an array or list or something that knows all the tiles, then you could just compare the position of the ship on a certain axis, and how far the ship is from the lowest row of tiles, then transport them if they are far enough.

    An alternative way of simply moving the space texture, would be to change the texture offset as the ship moves, but just have the background centered with the camera, so that it creates the sense the seamless texture is moving when it's really just the offset of the texture being changed. That won't work if your going to use alternative mixed textures (not just space but maybe fading into other different textures) but if your only going to have a single texture the whole game repeating, then it would be overkill to make a tile system that repositions, unless those background tiles play some important role in the gameplay, like being triggers or something.
     
  7. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    I didn't think about doing a texture offset as the ship moves, then the camera is locked to the ship and the stars. The goal is to make a large playing area without any massive maps.

    I had the thought of each update() calculate the distance of all 12 tiles and the ship, like you were saying too. This would be a lot easier if it was just a forward of sideways game where you would move tiles in 1 direction. if a ship moves up and sideways at the same time I would need to relocated the lower and left tiles to the top right side.

    I will do some experimenting this evening. I am liking the Collider as a trigger and using the Physics.OverLapBox to see if the the ships collider box which is very large and will expand off the camera view is still touching tile colliders. then check the direction the ship is moving and relocated the tiles in that direction. But I will look at the texture offset.

    Thanks!
     
    MD_Reptile likes this.
  8. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Good luck!
     
    larswik likes this.