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 awplays49 · Jul 01, 2015 at 09:24 PM · generationgenerating

Trying to make things far away deactivate. What can I do to this code to make it happen?

This code executes spawning random terrain in 2 sections. left and right, left being to the left of 0, right vice versa. Im sure there is an easier way to do this, because the code I have isn't working at all. renderL and renderR and so forth is supposed to keep track of the farthest render on each side. is there an easier way to do this? Before attempting to implement this, the chunks spawn when going near them, but now it's really broken due to my attempts.

-Edit-

I took out the parts I was working on because they were really broken, and I added comments

 using UnityEngine;
 using System;
 using System.Collections;
 
 public class TerrainGeneration : MonoBehaviour {
 
     public int width, height;
     [Range (0, 100)]
     public int fillPercent;
     private enum Cubes
     {
         No,
         Yes
     };
     private Cubes [,] map;
     private enum CubeTypes
     {
         Dirt,
         Grass,
         Cobblestone,
         DirtMoss,
         CobblestoneMoss
     };
     private CubeTypes [,] mapData;
     public string seed;
     public bool useRandomSeed;
     public int smoothness;
     public int edgeThickness;
     public GameObject [] cubePack_1;
     public GameObject player;
     public int chunkSize;
     public int leftmostChunk, rightmostChunk;
     public int hillStrength;
     public int hillHeight;
     public int hillSample;
     public int [] triggerDepthLevels;
     public int [] triggerHeightLevels;
     public GameObject chunkContainer;
     public int renderDistance;
     
     // This constructor checks blocks around the block being plugged in at x, y
     int GetNeighbors (int x, int y) { 
         int neighbors = 0;
         for (int x2 = x - 1; x2 < x + 2; x2 ++)
         {    
             for (int y2 = y - 1; y2 < y + 2; y2 ++)
             {
                 if (x2 != x || y2 != y)
                 {
                     if (x2 > -1 && x2 < width && y2 > -1 && y2 < height + hillHeight)
                     {
                         if (map [x2, y2] == Cubes.Yes)
                         {
                             neighbors ++;
                         }
                     }
                 }
             }
         }
         return neighbors;
     }
     
     // This constructor checks above the block until going up h times
     bool CheckAboveBlock (int x, int y, int h) { 
         if (height + hillHeight - y > h)
         {
             for (int y2 = y + 1; y2 < y + h + 1; y2 ++)    
             {
                 if (map [x, y2] == Cubes.Yes)
                 {
                     return true;
                 }
             }
         }
         else
         {
             for (int y2 = y + 1; y2 < height + hillHeight; y2 ++)    
             {
                 if (map [x, y2] == Cubes.Yes)
                 {
                     return true;
                 }
             }
         }
         return false;
     }
     
     void Start () {
         CreateGrid ();
     }
     
     void Update () {
         // This makes sure all the current rendering is done in one frame
         for (int i = 0; i < renderDistance; i ++) 
         {
             // If the player is beyond a certain distance and the whole map hasn't been renderered on that side
             if (player.transform.position.x < -leftmostChunk * chunkSize / 4 + chunkSize / 4 * renderDistance)
             {
                 if (leftmostChunk * chunkSize <= width / 2 - chunkSize)
                 {
                     CreateCubes (-1);
                 }
             }
             if (player.transform.position.x > rightmostChunk * chunkSize / 4 - chunkSize / 4 * renderDistance)
             {
                 if (rightmostChunk * chunkSize <= width / 2 - chunkSize)
                 {
                     CreateCubes (1);
                 }
             }
         }
     }
     
     // Creates arrays to contain block data
     void CreateGrid () { 
         map = new Cubes [width, height + hillHeight];
         mapData = new CubeTypes [width, height + hillHeight];
         Randomize ();
     }
     
     // Based on a seed, this places blocks and empty spaces based on chance of fill percentage and edge thickness
     void Randomize () { 
         if (useRandomSeed == true)
         {
             seed = Time.time.ToString ();
         }
         System.Random prng = new System.Random (seed.GetHashCode());
         for (int x = 0; x < width; x ++)
         {
             for (int y = 0; y < height; y ++)
             {
                 if (x < edgeThickness || x > width - edgeThickness - 1 || y < edgeThickness || y > height - edgeThickness - 1)
                 {
                     map [x, y] = Cubes.Yes;
                 }
                 else if (prng.Next (0, 100) < fillPercent)
                 {
                     map [x, y] = Cubes.Yes;
                 }
             }
         }    
         // Hills are pulled out of the ground s amount of times
         for (int s = 0; s < hillStrength; s ++) 
         {    
             int additiveHeight = prng.Next (hillSample, hillSample + 2);
             int newHeight = height + additiveHeight;
             BuildHills (newHeight);
         }
         // Smoothing occurs s2 amount of times to give more public flexibility
         for (int s2 = 0; s2 < smoothness; s2 ++) 
         {
             SmoothMap ();
         }
         DetermineCubes ();
         CreateCubes (-1);
         CreateCubes (1);
     }
     
     // If a block has mroe than 4 neighbors, it becomes a definite cube. If it has exactly 4, it is left alone. Less than 4, it is destroyed.
     void SmoothMap () {
         for (int x = 0; x < width; x ++)
         {
             for (int y = 0; y < height + hillHeight; y ++)
             {    
                 int neighbors = GetNeighbors (x, y);
                 if (neighbors > 4)
                 {
                     map [x, y] = Cubes.Yes;
                 }
                 else if (neighbors < 4)
                 {
                     map [x, y] = Cubes.No;
                 }
             }
         }
     }
     
     // This places some blocks above the ground that are later smoothed out. This is just an early implementation that will be changed later
     void BuildHills (int n) {
         System.Random prng = new System.Random (seed.GetHashCode ());
         for (int x = 0; x < width; x ++)
         {
             if (prng.Next (1, 4) == 1)
             {
                 for (int y = height - 1; y < height + hillHeight; y ++)
                 {
                     if (y < n)
                     {
                         map [x, y] = Cubes.Yes;
                     }
                 }
             }
         }
         for (int x = 0; x < width; x ++)
         {
             for (int y = height - 1; y < height + hillHeight; y ++)
             {
                 int neighbors = GetNeighbors (x, y);
                 if (neighbors == 0)
                 {
                     map [x, y] = Cubes.No;
                 }
             }
         }
     }
     
     // Based on the environment and circumstances of each cube, each space is put into an array of enums that says what each block is
     void DetermineCubes () { 
         for (int x = 0; x < width; x ++)
         {
             for (int y = 0; y < height + hillHeight; y ++)
             {
                 if (map [x, y] == Cubes.Yes)
                 {
                     int neighbors = GetNeighbors (x, y);
                     if (neighbors < 8)
                     {
                         if (y < height + triggerDepthLevels [0])
                         {
                             mapData [x, y] = CubeTypes.Cobblestone;
                         }
                     }
                     bool checkAbove = CheckAboveBlock (x, y, 5);
                     if (checkAbove == false)
                     {
                         if (y < height + triggerDepthLevels [1])
                         {
                             if (mapData [x, y] == CubeTypes.Dirt)
                             {
                                 mapData [x, y] = CubeTypes.DirtMoss;
                             }
                             else if (mapData [x, y] == CubeTypes.Cobblestone)
                             {
                                 mapData [x, y] = CubeTypes.CobblestoneMoss;
                             }
                         }
                         else if (mapData [x, y] == CubeTypes.Dirt)
                         {
                             mapData [x, y] = CubeTypes.Grass;
                         }
                     }
                 }
             }
         }
     }
     
     // This actually instantiates the cubes. Some of these fomulas are complicated due to the player starting in the middle of the map.
     void CreateCubes (int direction) {
         if (direction == -1)
         {
             GameObject currentChunk = Instantiate (chunkContainer, Vector3.zero, Quaternion.identity) as GameObject;
             currentChunk.gameObject.name = "Chunk Container " + "L" + (leftmostChunk + 1);
             for (int x = 0; x < chunkSize; x ++)
             {
                 for (int y = 0; y < height + hillHeight; y ++)
                 {
                     if (map != null && mapData != null)
                     {
                         if (map [width / 2 - chunkSize - leftmostChunk * chunkSize + x, y] == Cubes.Yes)
                         {
                             Vector2 pos = new Vector3 ((float) x / 4 - chunkSize / 4 - leftmostChunk * chunkSize / 4 + 0.125f, (float) y / 4 - height / 2 + 0.125f);
                             if (mapData [width / 2 - chunkSize - leftmostChunk * chunkSize + x, y] == CubeTypes.Dirt)
                             {
                                 GameObject currentCube = Instantiate (cubePack_1 [0], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                             }
                             else if (mapData [width / 2 - chunkSize - leftmostChunk * chunkSize + x, y] == CubeTypes.Grass)
                             {
                                 GameObject currentCube = Instantiate (cubePack_1 [1], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                             }
                             else if (mapData [width / 2 - chunkSize - leftmostChunk * chunkSize + x, y] == CubeTypes.Cobblestone)
                             {
                                 GameObject currentCube = Instantiate (cubePack_1 [2], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                             }
                             else if (mapData [width / 2 - chunkSize - leftmostChunk * chunkSize + x, y] == CubeTypes.DirtMoss)
                             {
                                 GameObject currentCube = Instantiate (cubePack_1 [0], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                                 currentCube = Instantiate (cubePack_1 [3], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                             }
                             else if (mapData [width / 2 - chunkSize - leftmostChunk * chunkSize + x, y] == CubeTypes.CobblestoneMoss)
                             {
                                 GameObject currentCube = Instantiate (cubePack_1 [2], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                                 currentCube = Instantiate (cubePack_1 [3], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                             }
                         }
                     }
                 }
             }
             leftmostChunk ++;
         }
         else if (direction == 1)
         {
             GameObject currentChunk = Instantiate (chunkContainer, Vector3.zero, Quaternion.identity) as GameObject;
             currentChunk.gameObject.name = "Chunk Container " + "R" + (rightmostChunk + 1);
             for (int x = 0; x < chunkSize; x ++)
             {
                 for (int y = 0; y < height + hillHeight; y ++)
                 {
                     if (map != null && mapData != null)
                     {
                         if (map [width / 2 + rightmostChunk * chunkSize + x, y] == Cubes.Yes)
                         {
                             Vector2 pos = new Vector3 ((float) x / 4 + rightmostChunk * chunkSize / 4 + 0.125f, (float) y / 4 - height / 2 + 0.125f);
                             if (mapData [width / 2 + rightmostChunk * chunkSize + x, y] == CubeTypes.Dirt)
                             {
                                 GameObject currentCube = Instantiate (cubePack_1 [0], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                             }
                             else if (mapData [width / 2 + rightmostChunk * chunkSize + x, y] == CubeTypes.Grass)
                             {
                                 GameObject currentCube = Instantiate (cubePack_1 [1], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                             }
                             else if (mapData [width / 2 + rightmostChunk * chunkSize + x, y] == CubeTypes.Cobblestone)
                             {
                                 GameObject currentCube = Instantiate (cubePack_1 [2], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                             }
                             else if (mapData [width / 2 + rightmostChunk * chunkSize + x, y] == CubeTypes.DirtMoss)
                             {
                                 GameObject currentCube = Instantiate (cubePack_1 [0], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                                 currentCube = Instantiate (cubePack_1 [3], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                             }
                             else if (mapData [width / 2 + rightmostChunk * chunkSize + x, y] == CubeTypes.CobblestoneMoss)
                             {
                                 GameObject currentCube = Instantiate (cubePack_1 [2], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                                 currentCube = Instantiate (cubePack_1 [3], pos, Quaternion.identity) as GameObject;
                                 currentCube.transform.parent = currentChunk.transform;
                             }
                         }
                     }
                 }
             }
             rightmostChunk ++;
         }
     }
 }
 
Comment
Add comment · Show 4
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
avatar image Xarbrough · Jul 01, 2015 at 11:04 PM 1
Share

Please do some prepwork and testing on your own and then post a much smaller chunk of code, which exactly states your problem. Nobody is willing to read and think about your whole class. Figure out what exactly is not working and make a new script that only tests this one thing. Usually this way you find the problem yourself, but then its also much easier to ask others. ;)

avatar image awplays49 · Jul 01, 2015 at 11:10 PM 0
Share

I tried so much, like for hours. I cant seem to find whats wrong.

All that is important is update and the last 3 voids at the bottom.

avatar image KdRWaylander · Jul 02, 2015 at 09:26 AM 0
Share

Some comments in your code would be apreciated too ^^

avatar image awplays49 · Jul 02, 2015 at 12:07 PM 0
Share

@$$anonymous$$dRWaylander. Here, I deleted the new parts I was working on because I found it useless to have broken code in there, and I put in comments

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

22 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

Related Questions

Generating random tower defense level by placing random square-shaped tiles? 1 Answer

how one would go about using Triangulator in javascript? 0 Answers

Procedural terrain generation ? 0 Answers

Dyanmic Platforms and Erasing Geometry? 0 Answers

Randomly Generated map 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