Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Terrain that repeats itself

Discussion in 'Scripting' started by gorbit99, Aug 8, 2015.

  1. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    I have a small terrain (out of cubes not a terrain GameObject).. I made the whole thing seamless, and parented all of the tiles to an empty GameObject. I need a way to repeat it over and over. My only problem is that the terrain is "editable" and teleporting the player isn't an option. How could I solve this?
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Make the whole "terrain" object a prefab (just drag the parent object down into your assets) and then you can instantiate it over and over- just offset the position of the new instance by whatever the width/length is so it snaps into place next to the first one instead of overlapping it. I have no idea what you mean by "editable" though.
     
  3. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    It's procedurally generated, so that's not an option, and did you read this part:
    it wouldn't help.
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    As I said in my post, I have no idea what you mean by "editable", or did you not read that part? Explain what you mean by "editable" and while you're at it give a more detailed explanation about what you've already got and what it is that you need, specifically. "A small terrain made out of cubes" is fine, but that doesn't necessarily mean "procedurally generated", so that was already a huge unmentioned addition to the problem. How are you generating this "procedurally generated" area and what have you tried so far in trying to copy it? Why would "teleporting the player" be relevant in a discussion of how to copy your terrain?
     
  5. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    By editable terrain I mean the player can interact with it, and because of this it may change at point (buildings will appear and change in the world). My terrain is made by placing cubes on a grid using the built-in perlin noise function and because it needs to be seamless (so it can repeat without a gap), I use a little bit of smoothing. this process is pretty fast and doesn't have much to say about. When this process is done, all the cubes get parented to an empty GameObject (called "Terrain" to be specific) because of performance issues (getting the childrens is faster than GameObject.FindGameObjectsWithTag).
    I have a basic way to solve the problem:
    1. After the generation, I parent every cube to it's corresponding section using it's coordinates
    2. I check the player's current position every second or so and I get the section, that he's in.
    3. I check if the section is changed.
    3.1 if it is, then with a switch-case statement, I place every section around the current one and then I store the current sections number for later checking.
    4. if everything's done, I go back to 2.​
    This is working fine, and you can't see the boundaries of the map, but every time a section change happens, the fps drops down from 60 to 15 and it's clearly visible. If you want, I can post this code, but you won't see anything I didn't mention. The other problem with this code, is that it's a good example of hard-coding, because I needed to code in exactly, where every section is going after a change. The player teleporting is a good method to use with non-editable terrains, it's basically just the same terrain repeated 9 times in a 3x3 grid, and whenever the player enters in the edges of the grid (so he's not in the middle one) the game teleports him to the corresponding part of the middle terrain, this way he can't exit from it or get to the edges. This is not possible in my case, because i would need to copy the middle terrain to all nine spaces if any change happens.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Are you looking for Instantiate? I don't get the problem.

    If you need to deal with the frame rate spike, just shift your Instantiate calls out across several frames.
     
  7. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    I have a terrain, it's generated, smoothed so it's seamless, and parented. I want to loop it around infinitely, so if you go in a direction, you can't see the end of the map, and you end up in the same place. Like on a planet, I don't instantiate anything, except for the generating phase. I want it to look like the player is running around on a sphere.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    So camera tricks? Or do you want an actual sphere? Or just keep instantiating the same terrain over and over again?

    I have no idea what you are actually asking for here.
     
  9. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    None of it, but the third one is closest to my idea, because i want to make it infinite, without a growing object count, if I would instantiate the terrain over and over again, then there will be so many objects in game, that the whole thing would just lag. And instantiating isn't the most performance friendly way of doing this, that's why I just move the corresponding parts of the terrain. Here's an image of this:

    The problem is, that this makes lag spikes whenever I move a section.
     

    Attached Files:

  10. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    451
    Simply keep 4 copies of the same terrain in the scene, and move them as the player moves around. (move the copies that are fartest away to the border of the terrain the player is walking towards).

    Note that if you never teleport the player, you could theoretically get problems with floating point precission. (assuming the terrains tile in a flat plane, and not on a sphericall shell)

    I'm working on something similar


    Edit:
    Oh wait, just saw your last post, you apparently already do this.
    Well I don't think there's really a better way to do it.
    You could try making the movement be done over multiple frames (to spread the performance-cost)
     
    Kiwasi likes this.
  11. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    I don't think that the floating point precission can be a huge problem, you need a huge patience to walk that far :D
     
  12. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    I solved it, it now move the cubes line by line, it has a much better performance, than moving them section by section, and the code is smaller too