Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Oct 13, 2014 at 05:26 PM · c#gameobjectlagslow

Infinite Terrain Dilemma. (Chunk Based System... Not A Minecraft Clone) [FIXED! PLEASE USE AS A REFERENCE.]

I cannot think of a solution. My problem is that my terrain gen is working but the chunks are not loading the way I want. They are only loaded beneath me, which makes sense because that is how I wrote the code but I do not know how to spawn more "chunks" in. Like, (i'll use minecraft as an example) when you move in the world, chunks infront of you become generated and the ones behind you removed. The problem is that in mine, chunks only spawn in when they are beneath you and then the last chunk gets deleted. There is also another problem, my chunk system makes the game very laggy and when a new chunk becomes generated, there is roughly a 7 second freeze. My computer isn't the best (far from it) but I know it is not supposed to be that bad.

I know my code is quite messy and poorly written, I didn't really pseudo-code but this could have been better. I didn't want to download any scripts or follow a youtube tut with this game. I want the effort put in. Idk, it feels more original that way. But Unity Answers is fine with me.

<<<<<<<<<<UPDATE>>>>>>>>>>

I fixed it in my own time.... I needed to practically revamp the code. I changed everything and here is what I have now. Not that the script is split into two segments. The Old Segment and The New Segment:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Chunks : MonoBehaviour {
 
     /*<<<<<<<<<<OLD CODE>>>>>>>>>>*/
 //    public GameObject SeaChunkMain;
 //    public GameObject SeaChunkSurr;
 //    float currentPosX;
 //    float currentPosZ;
 //    
 //    public List<Vector3> alreadySpawnedChunks = new List<Vector3>();
 //    
 //
 //    void Start()
 //    {
 //            Instantiate(SeaChunkMain, new Vector3(0, 0, 0), Quaternion.identity);
 //            alreadySpawnedChunks.Add(new Vector3(0, 0, 0));
 //            
 //            for(int x = 0; x < 20; x++)
 //            {
 //                for(int z = 0; z < 20; z++)
 //                {
 //                    if(!alreadySpawnedChunks.Contains(new Vector3(0 + x, 0, 0 + z)))
 //                    {
 //                        Instantiate(SeaChunkSurr, new Vector3(0 + x, 0, 0 + z), Quaternion.identity);
 //                        alreadySpawnedChunks.Add(new Vector3(0 + x, 0, 0 + z));
 //                    }
 //                    
 //                    if(!alreadySpawnedChunks.Contains(new Vector3(0 - x, 0, 0 + z)))
 //                    {
 //                        Instantiate(SeaChunkSurr, new Vector3(0 - x, 0, 0 + z), Quaternion.identity);
 //                        alreadySpawnedChunks.Add(new Vector3(0 - x, 0, 0 + z));
 //                    }
 //                    
 //                    if(!alreadySpawnedChunks.Contains(new Vector3(0 - x, 0, 0 - z)))
 //                    {
 //                        Instantiate(SeaChunkSurr, new Vector3(0 - x, 0, 0 - z), Quaternion.identity);
 //                        alreadySpawnedChunks.Add(new Vector3(0 - x, 0, 0 - z));
 //                    }
 //                    
 //                    if(!alreadySpawnedChunks.Contains(new Vector3(0 + x, 0, 0 - z)))
 //                    {
 //                        Instantiate(SeaChunkSurr, new Vector3(0 + x, 0, 0 - z), Quaternion.identity);
 //                        alreadySpawnedChunks.Add(new Vector3(0 + x, 0, 0 - z));
 //                    }
 //                }
 //            }
 //    }
 //
 //    void Update () 
 //    {
 //        currentPosX = Mathf.Round(transform.position.x);
 //        currentPosZ = Mathf.Round(transform.position.z);
 //        ChunkInstanciator(currentPosX, currentPosZ);
 //        ChunkInstanciator(currentPosX, currentPosZ);
 //    }
 //
 //    void ChunkInstanciator(float cPX, float cPZ) 
 //    {
 //        if(!alreadySpawnedChunks.Contains(new Vector3(cPX, 0, cPZ)))
 //        {
 //            if(GameObject.FindGameObjectsWithTag("Chunk") != null)
 //            {
 //                for(int i = 0; i < GameObject.FindGameObjectsWithTag("Chunk").Length; i++)
 //                {
 //                    alreadySpawnedChunks.Remove(GameObject.FindGameObjectsWithTag("Chunk")[i].transform.position);
 //                    Destroy(GameObject.FindGameObjectsWithTag("Chunk")[i]);
 //                }
 //            }
 //            Instantiate(SeaChunkMain, new Vector3(cPX, 0, cPZ), Quaternion.identity);
 //            alreadySpawnedChunks.Add(new Vector3(cPX, 0, cPZ));
 //
 //            for(int x = 0; x < 20; x++)
 //            {
 //                for(int z = 0; z < 20; z++)
 //                {
 //                    if(!alreadySpawnedChunks.Contains(new Vector3(cPX + x, 0, cPZ + z)))
 //                    {
 //                    Instantiate(SeaChunkSurr, new Vector3(cPX + x, 0, cPZ + z), Quaternion.identity);
 //                    alreadySpawnedChunks.Add(new Vector3(cPX + x, 0, cPZ + z));
 //                    }
 //
 //                    if(!alreadySpawnedChunks.Contains(new Vector3(cPX - x, 0, cPZ + z)))
 //                    {
 //                    Instantiate(SeaChunkSurr, new Vector3(cPX - x, 0, cPZ + z), Quaternion.identity);
 //                    alreadySpawnedChunks.Add(new Vector3(cPX - x, 0, cPZ + z));
 //                    }
 //
 //                    if(!alreadySpawnedChunks.Contains(new Vector3(cPX - x, 0, cPZ - z)))
 //                    {
 //                    Instantiate(SeaChunkSurr, new Vector3(cPX - x, 0, cPZ - z), Quaternion.identity);
 //                    alreadySpawnedChunks.Add(new Vector3(cPX - x, 0, cPZ - z));
 //                    }
 //
 //                    if(!alreadySpawnedChunks.Contains(new Vector3(cPX + x, 0, cPZ - z)))
 //                    {
 //                    Instantiate(SeaChunkSurr, new Vector3(cPX + x, 0, cPZ - z), Quaternion.identity);
 //                    alreadySpawnedChunks.Add(new Vector3(cPX + x, 0, cPZ - z));
 //                    }
 //                }
 //            }
 //        }
 //    }
 
     /*<<<<<<<<<<NEW CODE>>>>>>>>>>*/
     public GameObject SeaGO;
     public List<Vector3> chunks = new List<Vector3>();
 
     float PX;
     float PZ;
 
     public float chunkSize = 4;
 
     void Start()
     {
         ChunkInst(0, 0);
     }
 
     void Update()
     {
         PX = Mathf.Round(transform.position.x);
         PZ = Mathf.Round(transform.position.z);
         ChunkInst(PX, PZ);
     }
 
     void ChunkInst(float valueX, float valueZ)
     {
         //Destroys
         for(int d = 0; d < GameObject.FindGameObjectsWithTag("Chunk").Length; d++)
         {
             Destroy(GameObject.FindGameObjectsWithTag("Chunk")[d]);
             chunks.Clear(); 
         }
 
         for(int i = 0; i < chunkSize; i++)
         {
             for(int z = 0; z < chunkSize; z++)
             {
 //            chunks.Add(Instantiate(SeaGO, new Vector3(valueX - i, 0, valueZ), Quaternion.identity) as GameObject);
 //            chunks.Add(Instantiate(SeaGO, new Vector3(valueX + i, 0, valueZ), Quaternion.identity) as GameObject);
 //            chunks.Add(Instantiate(SeaGO, new Vector3(valueX, 0, valueZ - i), Quaternion.identity) as GameObject);
 //            chunks.Add(Instantiate(SeaGO, new Vector3(valueX, 0, valueZ + i), Quaternion.identity) as GameObject);
 
                 if(!chunks.Contains(new Vector3(valueX + i * 10, 0, valueZ + z * 10)))
                 {
                 Instantiate(SeaGO, new Vector3(valueX + i * 10, 0, valueZ + z * 10), Quaternion.identity);
                 chunks.Add(new Vector3(valueX + i * 10, 0, valueZ + z * 10));
                 }
 
                 if(!chunks.Contains(new Vector3(valueX - i * 10, 0, valueZ - z * 10)))
                 {
                 Instantiate(SeaGO, new Vector3(valueX - i * 10, 0, valueZ - z * 10), Quaternion.identity);
                 chunks.Add(new Vector3(valueX - i * 10, 0, valueZ - z * 10));
                 }
 
                 if(!chunks.Contains(new Vector3(valueX + i * 10, 0, valueZ - z * 10)))
                 {
                 Instantiate(SeaGO, new Vector3(valueX + i * 10, 0, valueZ - z * 10), Quaternion.identity);
                 chunks.Add(new Vector3(valueX + i * 10, 0, valueZ - z * 10));
                 }
 
                 if(!chunks.Contains(new Vector3(valueX - i * 10, 0, valueZ + z * 10)))
                 {
                 Instantiate(SeaGO, new Vector3(valueX - i * 10, 0, valueZ + z * 10), Quaternion.identity);
                 chunks.Add(new Vector3(valueX - i * 10, 0, valueZ + z * 10));
                 }
 
             }
         }
     }
 }

Also note that the newer version runs alot faster.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by $$anonymous$$ · Oct 13, 2014 at 06:57 PM

The Gameobject's Scale Has To Be: X: 1, Y: Whatever (Recommended 0.1f), Z: 1

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

destroying game object in a specific transform 2 Answers

Multiple Cars not working 1 Answer

C# Slow Down all Gameobjects Within another Gameobject's Collider 2 Answers

DontDestroyOnLoad is works on Scenes in UNITY3D? 2 Answers

Method for checking if a key is pressed without slowing game. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges