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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Terrain chunking in unity 3D

Discussion in 'Editor & General Support' started by KimMingyuSVT, Sep 17, 2020.

  1. KimMingyuSVT

    KimMingyuSVT

    Joined:
    May 28, 2019
    Posts:
    7
    I want to make a terrain mechanism that will work according to my character's position. I am only working on the y-axis. And the fundamental composition of my game is, that my character(a sphere) goes up in the y-axis on tapping, and as I am on the y-axis as soon as I stop tapping no force is applied in an upward direction and gravity kicks in so the sphere starts falling.

    What I basically want to do is, I have made Crossy road type roads and the sphere will go up and there are cars incoming from the left and right direction on these roads(terrains) and I want that as soon as my player goes up and gets to a specific position, say (0,10) I want the terrain mechanism to kick in and lock the gap from the first and last terrain, like at (0,10) position of sphere, there are 10 terrains below it and 10 above it. And from now on I want it to go up in chunks, like say if the player moved to (0,12) then the terrain at (0,1) gets destroyed and new one spawn at (0,2), and the same happen at the top. Kind of like min and max, like if position is (0,15) min = (0,5) and max = (0,25).

    All my terrains are prefabs.

    I am kinda bad at explaining so I explained in the word file below. Please check Please answer, I would really appreciate it. Please a coding help is what I need please.

    [Detailed] https://drive.google.com/file/d/1iDmaq-8hGVPNldbpM81axYaJ1PXVcDoo/view?usp=drivesdk
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What's wrong with just instantiating/positioning and destroying the terrains as needed? Otherwise I'm not understanding the actual problem.
     
  3. KimMingyuSVT

    KimMingyuSVT

    Joined:
    May 28, 2019
    Posts:
    7
    I mean I am actually looking for instantiating and deleting, but I hope you got the part where I am saying that, I want to kind of make a terrain mechanism which will kick in after my char(a sphere) goes up (in y-axis) a certain distance say (0,10) then the mechanism should kick in and always maintain the gap from the very first terrain and the very last.
    Like 10 terrains below and 10 above my sphere and from now on it maintains the gap, like if the sphere go up say (0,12) then delete the terrain at (0,1) and spawn at (0,2).
    But like say I'm tapping and sphere goes up and down at same position and doesn't move up then the terrain should not be deleted. I hope you understood, please ask me if you didn't.
     
  4. KimMingyuSVT

    KimMingyuSVT

    Joined:
    May 28, 2019
    Posts:
    7
    Hello, can you please help? I been searching for a long time now but couldn't find anywhere
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm not really sure I get this. Are you having trouble just writing the script which detects if the character has gone up by 10 units? You can do this all in code, or you probably can just use trigger colliders. If in code you would just take the starting position and save it. Then every frame check if the player's current Y position is either greater or less than 10 from the original position. If greater than 10, you add 10 to the original Y position, if less then 10 you subtract 10 from the original Y position. Then just destroy the terrain needing to get destroyed (not sure if that is what you need) and instantiate the new terrain prefab at the correct position.