Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Breaking_Reality · Aug 01, 2019 at 02:40 PM · optimizationlaginfinite terrain

Any help on optimizing this infinite terrain script?

Hello this is my infinite terrain script for my game. It works but its incredibly laggy when the player moves around but it does work. I will not share my chunk script because its not necessary because nothing really is called on the chunks other then updating position. NOTE: I know this script is not very good but I tried based on what I know which is why I came here for help to learn more :D thanks

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GEN_InfiniteTerrain : MonoBehaviour
 {
     public GameObject targetObject;
     public GameObject chunkObject;
     public int chunkSize;
     public float unitSize;
 
     public int renderDistance;
     Dictionary<Vector2, GameObject> gridOfChunks = new Dictionary<Vector2, GameObject>();
     List<Vector2> expectedChunkGridPositions = new List<Vector2>();
 
     public float noiseScale;
 
     // Infinite terrain values
     float absoluteChunkSize;
 
     private void Start()
     {
         // Calculate absolute chunk size
         GetAbsoluteChunkSize();
 
         // Generate base world
         GenerateBase();
     }
 
     Vector2 lastTargetGridPosition = Vector2.zero;
     private void LateUpdate()
     {
         // Get the targets position in world space
         Vector3 targetAbsolutePosition = targetObject.transform.position;
 
         // Convert the targets world position to grid position (/ 10 * 10 is just rounding to 10)
         Vector2 targetGridPosition = new Vector2();
         targetGridPosition.x = Mathf.RoundToInt(targetAbsolutePosition.x / 10) * 10 / absoluteChunkSize;
         targetGridPosition.y = Mathf.RoundToInt(targetAbsolutePosition.z / 10) * 10 / absoluteChunkSize;
 
         if (targetGridPosition - lastTargetGridPosition != Vector2.zero)
         {
             GenerateExpectedChunkAreas(targetGridPosition);
             UpdateChunkPositions(targetGridPosition);
         }
 
         lastTargetGridPosition = targetGridPosition;
     }
 
     void GenerateBase()
     {
         for (int x = -renderDistance / 2; x < renderDistance / 2; x++)
         {
             for (int z = -renderDistance / 2; z < renderDistance / 2; z++)
             {
                 Vector2 gridPosition = new Vector2(x, z);
                 Vector3 worldPosition = new Vector3(x * (unitSize * chunkSize), 0, z * (unitSize * chunkSize));
                 GameObject chunk = Instantiate(chunkObject, worldPosition, Quaternion.identity);
                 chunk.GetComponent<GEN_Chunk>().gridPosition = gridPosition;
                 gridOfChunks.Add(gridPosition, chunk);
             }
         }
         GenerateExpectedChunkAreas(Vector2.zero);
     }
 
     void GenerateExpectedChunkAreas(Vector2 targetGridPosition)
     {
         expectedChunkGridPositions.Clear();
         for (int x = -renderDistance / 2; x < renderDistance / 2; x++)
         {
             for (int z = -renderDistance / 2; z < renderDistance / 2; z++)
             {
                 Vector2 gridPosition = new Vector2(x, z) + targetGridPosition;
                 expectedChunkGridPositions.Add(gridPosition);
             }
         }
     }
 
     void UpdateChunkPositions(Vector2 targetGridPosition)
     {
         List<Vector2> positionsWithoutChunks = new List<Vector2>();
         List<Vector2> positionsWithOldChunks = new List<Vector2>();
 
         for (int chunkCount = 0, x = -renderDistance / 2; x < renderDistance / 2; x++)
         {
             for (int z = -renderDistance / 2; z < renderDistance / 2; z++)
             {
                 Vector2 gridPosition = new Vector2(x, z) + targetGridPosition;
                 
                 if(!gridOfChunks.ContainsKey(gridPosition))
                 {
                     positionsWithoutChunks.Add(gridPosition);
                 }
                 chunkCount++;
             }
         }
 
         foreach (GameObject chunk in gridOfChunks.Values)
         {
             if(!expectedChunkGridPositions.Contains(chunk.GetComponent<GEN_Chunk>().gridPosition))
             {
                 positionsWithOldChunks.Add(chunk.GetComponent<GEN_Chunk>().gridPosition);
             }
         }
 
         for (int i = 0; i < positionsWithOldChunks.Count; i++)
         {
             Vector3 worldPosition = new Vector3(positionsWithoutChunks[i].x * absoluteChunkSize, 0, positionsWithoutChunks[i].y * absoluteChunkSize);
             gridOfChunks[positionsWithOldChunks[i]].transform.position = worldPosition;
 
             // Recalculating noise for chunk based on its new position does lag more but even WITHOUT this it still stutters when player moves around. ( plan to learn threading just to calculate noise on seperate threads )
            // gridOfChunks[positionsWithOldChunks[i]].GetComponent<GEN_Chunk>().ApplyNoise();
         }
     }
 
     void GetAbsoluteChunkSize()
     {
         absoluteChunkSize = unitSize * chunkSize;
     }
 }
 

 
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

0 Replies

· Add your reply
  • Sort: 

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

185 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 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 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 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 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 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 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

Lag Spike using Particle System Emit 0 Answers

Semaphore Wait for signal Slows Down My Simple Android Game 0 Answers

Gfx.WaitForPresent rant, and editor fix 0 Answers

Optimize Terrain In Infinite Runner 0 Answers

Strange framerate differance in game & profiler on GearVR. 0 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