Search Unity

can this script work and if not, how can i fix it?

Discussion in 'Scripting' started by ariusdb222, Feb 27, 2015.

  1. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    this is basically supposed to make biomes on terrain....


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class biome : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         // humidity from 0 to 1
    10.         float humidity = Mathf.PerlinNoise (transform.position.x * 0.001f, transform.position.y * 0.001f) * 0.5f + 0.5f;
    11.         // temperature from -30 to 50
    12.         float temp = Mathf.PerlinNoise (transform.position.x * 0.001f + 500, transform.position.y * 0.001f + 500) * 40 + 10f;
    13.        
    14.         if (temp < 0 && humidity > 0.4f) {
    15.             // thundra with lots of snow
    16.         } else if (temp < 0 && humidity <= 0.4f) {
    17.             // rocky and cold
    18.         } else if (temp >= 0 && humidity > 0.7) {
    19.             // swamp
    20.         } else if (temp > 30  && humidity < 0.4) {
    21.             // desert
    22.         } else if (humidity > 0.5) {
    23.             // rainforest
    24.         } else {
    25.             // grasslands
    26.         }
    27.  
    28.    
    29.     }
    30.    
    31.     // Update is called once per frame
    32.     void Update () {
    33.    
    34.     }
    35. }
    36.  
     
  2. Deleted User

    Deleted User

    Guest


    can this script work”?

    Why don't you try it out???
     
  3. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    i'm still figuring out the best way to get the biomes loaded.... like maybe a prefab?
    could it possibly work to load a TC prefab for the different biomes?
    also im not a programmer so i have no idea if this is going to work at all, just found this on the web and tried customizing it a bit.
    My real question though, is this even gonna work?

    thanks for your time...
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Hitting the play button is generally your fastest bet. If nothing unexpected happens you are good to go.

    As it stands this script does exactly nothing. Nada. Zip.

    It runs perlin noise on the objects current position to determine the humidity and temperature. It uses these numbers to determine what biome the object is in. Then it does absolutely nothing with it. Up to this point the code is sound. Assuming you can convert this into some sort of biome then you are away laughing.

    As a structure improvement I would recommend moving this off into a static helper class. I'd also suggest refractoring out the temperature and humidity methods. One can assume you would be interested in these outside of just biome calculations. I've also set up an enum to record your BiomeTypes. Looks something like this. Not tested for errors.

    Code (CSharp):
    1. public enum BiomeType {tundra, rocky, swamp, desert, rainforest, grasslands}
    2.  
    3. public static class BiomeHelperMethods {
    4.  
    5.    public static BiomeType DetirmineBiome (float x, float y) {
    6.         float humidity = GetHumidity (x, y);
    7.         float temp = GetTemperature (x,y);
    8.      
    9.         if (temp < 0 && humidity > 0.4f) {
    10.             return BiomeType.tundra;
    11.         } else if (temp < 0 && humidity <= 0.4f) {
    12.             return BiomeType.rocky;
    13.         } else if (temp >= 0 && humidity > 0.7) {
    14.             return BiomeType.swamp;
    15.         } else if (temp > 30  && humidity < 0.4) {
    16.             return BiomeType.desert;
    17.         } else if (humidity > 0.5) {
    18.             return BiomeType.rainforest;
    19.         } else {
    20.             return BiomeType.grasslands;
    21.         }
    22.     }
    23.  
    24.     public static float GetTemperature (float x, float y){
    25.         return Mathf.PerlinNoise (x * 0.001f + 500, y * 0.001f + 500) * 40 + 10f;
    26.     }
    27.  
    28.     public static float GetHumidity (float x, float y){
    29.         return Mathf.PerlinNoise (x * 0.001f, y * 0.001f) * 0.5f + 0.5f;
    30.     }
    31. }
    32.  
    The use case would be something like

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class biome : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         BiomeType myBiomeType = BiomeHelperMethods.DetirmineBiome (transform.positon.x, transform.position.y);
    10.         Debug.Log ("Wow, look at the view. I'm standing in a lovely " + myBiomeType.ToString());
    11.     }
    12. }
    13.  
    Hope that helps.
     
  5. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    Ok thanks for your time explaining how this works, i will try what you suggested later today.
    again thanks for your time!:)
     
  6. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    Ok so i looked at the code and makes a lot more sense than what i had in the beginning.
    My only question now is, is it possible to load a prefab for the given biome type... say biome type is = to biome.rainforest then load the rainforest prefab?
    How exactly would you go on implementing this to the code?
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Have a look at Instantiate. Or put all the assets into the scene, have them disabled and simply enable the one you need based on the biome type. You might have to figure out what's the best in your case as we don't know how large and how many objects belong to the biomes.
     
  8. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    so will something like this work??

    Code (CSharp):
    1.  
    2.           if (temp < 0 && humidity > 0.4f) {
    3.             return BiomeType.tundra;
    4.             GameObject.Instantiate (Resources.Load ("tundra")); ;
    5.         } else if (temp < 0 && humidity <= 0.4f) {
    6.             return BiomeType.rocky;
    7.             GameObject.Instantiate (Resources.Load ("rocky")); ;
    8.         } else if (temp >= 0 && humidity > 0.7) {
    9.             return BiomeType.swamp;
    10.             GameObject.Instantiate (Resources.Load ("swamp")); ;
    11.         } else if (temp > 30  && humidity < 0.4) {
    12.             return BiomeType.desert;
    13.             GameObject.Instantiate (Resources.Load ("desert")); ;
    14.         } else if (humidity > 0.5) {
    15.             return BiomeType.rainforest;
    16.             GameObject.Instantiate (Resources.Load ("rainforest")); ;
    17.         } else {
    18.             return BiomeType.grasslands;
    19.             GameObject.Instantiate (Resources.Load ("grasslands")); ;
    20.         }
    with Resources.Load(biome)
    the biome is a prefab.
    Will this work??
     
    Last edited: Feb 28, 2015
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I think it could work, but i would create variables and link the prefabs in the inspector. It's easier to maintain.
     
  10. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    To extent on this, The best way I guess, is to have a Base Biome class, and then set up a List<Biome> which contains the biomes. Then get the correct biome based on the enum:
    Code (CSharp):
    1. public class Biome: MonoBehaviour
    2. {
    3.     public BiomeType type;
    4.  
    5.     // Common Biome implementation
    6. }
    7.  
    8. public class Tundra : Biome
    9. {
    10.     // Specific Tundra Implementation
    11. }
    12.  
    13.  
    14. // Add this class to a gameobject (like a manager)
    15. public class BiomesCollection : Monobehaviour
    16. {
    17.     public List<Biome> biomes;
    18. }
    Now, in the class that instantiates the correct biome, just add a reference to the biomes list, and in the methis that instantiates the biome you can call:
    Code (CSharp):
    1. GameObject.Instantiate(biomes.where(b => b.type == myBiomeType), transformposition, transformrotation)
     
  11. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    Ok thanks for the suggestions guys, will do what you say as soon as i fix my script to actually work..
    could you tell me why this doesn't make different terrains??
    like i said, im not a programmer. just want to make this one thing work...


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class biome : MonoBehaviour {
    6.     void Start () {
    7.         BiomeType myBiomeType = BiomeHelperMethods.DetirmineBiome (transform.position.x, transform.position.y);
    8.         Debug.Log ("Wow, look at the view. I'm standing in a lovely " + myBiomeType.ToString());
    9.     }
    10. }
    11.  
    12. public enum BiomeType {tundra, rocky, swamp, desert, rainforest, grasslands}
    13.  
    14. public static class BiomeHelperMethods {
    15.  
    16.     public static BiomeType DetirmineBiome (float x, float y) {
    17.         float humidity = GetHumidity (x, y);
    18.         float temp = GetTemperature (x,y);
    19.      
    20.         if (temp < 0 && humidity > 0.4f) {
    21.             return BiomeType.tundra;
    22.             GameObject.Instantiate (Resources.Load ("tundra")); ;
    23.         } else if (temp < 0 && humidity <= 0.4f) {
    24.             return BiomeType.rocky;
    25.             GameObject.Instantiate (Resources.Load ("rocky")); ;
    26.         } else if (temp >= 0 && humidity > 0.7) {
    27.             return BiomeType.swamp;
    28.             GameObject.Instantiate (Resources.Load ("swamp")); ;
    29.         } else if (temp > 30  && humidity < 0.4) {
    30.             return BiomeType.desert;
    31.             GameObject.Instantiate (Resources.Load ("desert")); ;
    32.         } else if (humidity > 0.5) {
    33.             return BiomeType.rainforest;
    34.             GameObject.Instantiate (Resources.Load ("rainforest")); ;
    35.         } else {
    36.             return BiomeType.grasslands;
    37.             GameObject.Instantiate (Resources.Load ("grasslands")); ;
    38.         }
    39.     }
    40.  
    41.     public static float GetTemperature (float x, float y){
    42.         return Mathf.PerlinNoise (x * 0.001f + 500, y * 0.001f + 500) * 40 + 10f;
    43.     }
    44.  
    45.     public static float GetHumidity (float x, float y){
    46.         return Mathf.PerlinNoise (x * 0.001f, y * 0.001f) * 0.5f + 0.5f;
    47.     }
    48. }
    with the GameObject.Instantiate in the If it doesn't work...
    and when it is out of the If, it keeps on making new terrains...(like some sort of inifinte loop)
     
  12. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    You Instantiate a biome after returning the enum value, so the Instantiate within the if statements never gets called.
     
  13. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    So I must instantiate the biome first, then return the enum value?
     
  14. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    It is better to not instantiate anything there, as the example given was for a helper method that just returns the current biome.

    What you could do is the following:
    Code (CSharp):
    1. GameObject.Instantiate(
    2.     biomes.FirstOrDefault(b =>
    3.         b.type == BiomeHelperMethods.DetirmineBiome(
    4.             transform.positon.x,
    5.             transform.position.y)),
    6.     instancePosition,
    7.     instancerRotation);
     
  15. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    Ok thanks for pointing that out. So what else is needed to make the code load the biome prefabs? Or will everything be right after I change the instantiate??

    Also, I would like to thank Timelog, Suddoha and BoredMormon for all the help and your time. You guys are awesome! :)
     
    Last edited: Feb 28, 2015
  16. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Couple of points to note. Anything after a return statement does not happen. You should have a console warning about unreachable code detected?

    I was intending something like this. This will create a 10 by 10 square with the correct prefabs instantiated.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BiomeGenerator : MonoBehaviour {
    6.     void Start () {
    7.         for (int x = 0; x < 10; x++){
    8.             for (int y = 0; y < 10; y++){
    9.                 BiomeType myBiomeType = BiomeHelperMethods.DetirmineBiome (x, y);
    10.                 GameObject.Instantiate (Resources.Load (myBiomeType.ToString()), new Vector3 (x,y,0), Quarterion.Identity);
    11.             }
    12.         }
    13.     }
    14. }
    15.  
    16. // BiomeHelperMethods as written in my previous post
    17.  
     
    Last edited: Feb 28, 2015
  17. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    Again thanks for the help, but now this gives off errors that i have no idea how to fix.

    Assets/biome.cs(6,40): error CS1525: Unexpected symbol `x'
    Assets/biome.cs(7,48): error CS1525: Unexpected symbol `x'
    Assets/biome.cs(8,96): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
    Assets/biome.cs(8,96): error CS1525: Unexpected symbol `)'
    Assets/biome.cs(10,25): error CS1525: Unexpected symbol `}'
    Assets/biome.cs(16,37): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

    and some more errors, 14 in total
     
  18. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Never just copy over code and expect it to work, always read over it and try to understand what it does. Apart from learning from that, it will also help remove potential errors (I highly suggest writing it yourself instead of copying it).

    He forgot a couple of ';' symbols, which reflect your errors.
     
    Kiwasi likes this.
  19. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That's what comes of writing code in explorer. I think I've caught the compiler errors this time.

    Errors were missing semi colons and using x in inner loop that should have read y.
     
  20. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    The errors are gone, but now unity freezes when i click play

    here is the script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BiomeGenerator : MonoBehaviour {
    5.     void Start () {
    6.         for (int x = 0; x < 10; x++){
    7.             for (int y = 0; y < 10; y++){
    8.                 BiomeType myBiomeType = BiomeHelperMethods.DetirmineBiome (x, y);
    9.                 GameObject.Instantiate (Resources.Load (myBiomeType.ToString()), new Vector3 (x,y,0), Quaternion.identity);
    10.             }
    11.         }
    12.     }
    13. }
    14. public enum BiomeType {tundra, rocky, swamp, desert, rainforest, grasslands}
    15. public static class BiomeHelperMethods {
    16.    
    17.     public static BiomeType DetirmineBiome (float x, float y) {
    18.         float humidity = GetHumidity (x, y);
    19.         float temp = GetTemperature (x,y);
    20.        
    21.         if (temp < 0 && humidity > 0.4f) {
    22.             return BiomeType.tundra;
    23.         } else if (temp < 0 && humidity <= 0.4f) {
    24.             return BiomeType.rocky;
    25.         } else if (temp >= 0 && humidity > 0.7) {
    26.             return BiomeType.swamp;
    27.         } else if (temp > 30  && humidity < 0.4) {
    28.             return BiomeType.desert;
    29.         } else if (humidity > 0.5) {
    30.             return BiomeType.rainforest;
    31.         } else {
    32.             return BiomeType.grasslands;
    33.         }
    34.     }
    35.    
    36.     public static float GetTemperature (float x, float y){
    37.         return Mathf.PerlinNoise (x * 0.001f + 500, y * 0.001f + 500) * 40 + 10f;
    38.     }
    39.    
    40.     public static float GetHumidity (float x, float y){
    41.         return Mathf.PerlinNoise (x * 0.001f, y * 0.001f) * 0.5f + 0.5f;
    42.     }
    43. }
     
  21. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    A freeze indicates an infinite loop.

    You don't have this script attached to the prefabs in the resource folder by any chance?
     
  22. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    No the script is on the player camera. Or is it supposed to be attached somewhere else??
     
  23. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'm at a loss then. Are there any scripts at all on your prefabs in the resources folder?

    Or do you have pause on error enabled in the console?
     
  24. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    None of the above.
    damn wish i can get this thing working its the only thing i still need to really start with the game... biomes is one of the important features..:(
     
  25. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I may have to open up unity myself to try the script. Otherwise I'm at a loss for suggestions.
     
  26. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    Think it has something to do with this....

    Code (CSharp):
    1. void Start () {
    2.         for (int x = 0; x < 10; x++){
    3.             for (int y = 0; y < 10; y++){
    4.                 BiomeType myBiomeType = BiomeHelperMethods.DetirmineBiome (x, y);
    5.                 GameObject.Instantiate (Resources.Load (myBiomeType.ToString()), new Vector3 (x,y,0), Quaternion.identity);
    6.             }
    7.         }
    8.     }
    maybe a stupid question... but don't for also need break?
    i tried to use the break and it doesn't freeze anymore..


    this seems to work

    Code (CSharp):
    1. public class BiomeGenerator : MonoBehaviour {
    2.     void Start () {
    3.         for (int x = 0; x < 10; x++){
    4.             for (int y = 0; y < 10; y++){
    5.                 BiomeType myBiomeType = BiomeHelperMethods.DetirmineBiome (x, y);
    6.                 GameObject.Instantiate (Resources.Load (myBiomeType.ToString()), new Vector3 (x,y,0), Quaternion.identity);
    7.                 break;}
    8.             break;}
    9.     }
     
    Last edited: Mar 1, 2015
  27. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    Ok it seems to be fine.. will only know when it gets tested on the terrain.
    which brings me to another question, probably the last..

    How can i make this biome script work with the infinite terrain script?
    do i somehow attach the infinite terrain script or do i rewrite the entire script to have both scripts in one??
    Basically i already have a terrain.
    On the terrain, i have a infinite terrain script attached and the player controller connected.
    Now what i want is to add the biome script to make the different biomes on the infinite terrain.
    And i have no idea how i'm gonna do it....:confused:

    here is both..

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BiomeGenerator : MonoBehaviour {
    6.     void Start () {
    7.         for (int x = 0; x < 10; x++){
    8.             for (int y = 0; y < 10; y++){
    9.                 BiomeType myBiomeType = BiomeHelperMethods.DetirmineBiome (x, y);
    10.                 GameObject.Instantiate (Resources.Load (myBiomeType.ToString()), new Vector3 (x,y,0), Quaternion.identity);
    11.                 break;}
    12.             break;}
    13.     }
    14. }
    15. public enum BiomeType {tundra, rocky, swamp, desert, rainforest, grasslands}
    16. public static class BiomeHelperMethods {
    17.  
    18.     public static BiomeType DetirmineBiome (float x, float y) {
    19.         float humidity = GetHumidity (x, y);
    20.         float temp = GetTemperature (x,y);
    21.      
    22.         if (temp < 0 && humidity > 0.4f) {
    23.             return BiomeType.tundra;
    24.         } else if (temp < 0 && humidity <= 0.4f) {
    25.             return BiomeType.rocky;
    26.         } else if (temp >= 0 && humidity > 0.7) {
    27.             return BiomeType.swamp;
    28.         } else if (temp > 30  && humidity < 0.4) {
    29.             return BiomeType.desert;
    30.         } else if (humidity > 0.5) {
    31.             return BiomeType.rainforest;
    32.         } else {
    33.             return BiomeType.grasslands;
    34.         }
    35.     }
    36.  
    37.     public static float GetTemperature (float x, float y){
    38.         return Mathf.PerlinNoise (x * 0.001f + 500, y * 0.001f + 500) * 40 + 10f;
    39.     }
    40.  
    41.     public static float GetHumidity (float x, float y){
    42.         return Mathf.PerlinNoise (x * 0.001f, y * 0.001f) * 0.5f + 0.5f;
    43.     }
    44. }



    and infinite terrain(found this on through google)

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class InfiniteTerrain : MonoBehaviour
    6. {
    7.     public GameObject PlayerObject;
    8.  
    9.     private Terrain[,] _terrainGrid = new Terrain[3,3];
    10.  
    11.     void Start ()
    12.     {
    13.         Terrain linkedTerrain = gameObject.GetComponent<Terrain>();
    14.      
    15.         _terrainGrid[0,0] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
    16.         _terrainGrid[0,1] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
    17.         _terrainGrid[0,2] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
    18.         _terrainGrid[1,0] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
    19.         _terrainGrid[1,1] = linkedTerrain;
    20.         _terrainGrid[1,2] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
    21.         _terrainGrid[2,0] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
    22.         _terrainGrid[2,1] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
    23.         _terrainGrid[2,2] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
    24.  
    25.         UpdateTerrainPositionsAndNeighbors();
    26.     }
    27.  
    28.     private void UpdateTerrainPositionsAndNeighbors()
    29.     {
    30.         _terrainGrid[0,0].transform.position = new Vector3(
    31.             _terrainGrid[1,1].transform.position.x - _terrainGrid[1,1].terrainData.size.x,
    32.             _terrainGrid[1,1].transform.position.y,
    33.             _terrainGrid[1,1].transform.position.z + _terrainGrid[1,1].terrainData.size.z);
    34.         _terrainGrid[0,1].transform.position = new Vector3(
    35.             _terrainGrid[1,1].transform.position.x - _terrainGrid[1,1].terrainData.size.x,
    36.             _terrainGrid[1,1].transform.position.y,
    37.             _terrainGrid[1,1].transform.position.z);
    38.         _terrainGrid[0,2].transform.position = new Vector3(
    39.             _terrainGrid[1,1].transform.position.x - _terrainGrid[1,1].terrainData.size.x,
    40.             _terrainGrid[1,1].transform.position.y,
    41.             _terrainGrid[1,1].transform.position.z - _terrainGrid[1,1].terrainData.size.z);
    42.      
    43.         _terrainGrid[1,0].transform.position = new Vector3(
    44.             _terrainGrid[1,1].transform.position.x,
    45.             _terrainGrid[1,1].transform.position.y,
    46.             _terrainGrid[1,1].transform.position.z + _terrainGrid[1,1].terrainData.size.z);
    47.         _terrainGrid[1,2].transform.position = new Vector3(
    48.             _terrainGrid[1,1].transform.position.x,
    49.             _terrainGrid[1,1].transform.position.y,
    50.             _terrainGrid[1,1].transform.position.z - _terrainGrid[1,1].terrainData.size.z);
    51.      
    52.         _terrainGrid[2,0].transform.position = new Vector3(
    53.             _terrainGrid[1,1].transform.position.x + _terrainGrid[1,1].terrainData.size.x,
    54.             _terrainGrid[1,1].transform.position.y,
    55.             _terrainGrid[1,1].transform.position.z + _terrainGrid[1,1].terrainData.size.z);
    56.         _terrainGrid[2,1].transform.position = new Vector3(
    57.             _terrainGrid[1,1].transform.position.x + _terrainGrid[1,1].terrainData.size.x,
    58.             _terrainGrid[1,1].transform.position.y,
    59.             _terrainGrid[1,1].transform.position.z);
    60.         _terrainGrid[2,2].transform.position = new Vector3(
    61.             _terrainGrid[1,1].transform.position.x + _terrainGrid[1,1].terrainData.size.x,
    62.             _terrainGrid[1,1].transform.position.y,
    63.             _terrainGrid[1,1].transform.position.z - _terrainGrid[1,1].terrainData.size.z);
    64.      
    65.         _terrainGrid[0,0].SetNeighbors(             null,              null, _terrainGrid[1,0], _terrainGrid[0,1]);
    66.         _terrainGrid[0,1].SetNeighbors(             null, _terrainGrid[0,0], _terrainGrid[1,1], _terrainGrid[0,2]);
    67.         _terrainGrid[0,2].SetNeighbors(             null, _terrainGrid[0,1], _terrainGrid[1,2],              null);
    68.         _terrainGrid[1,0].SetNeighbors(_terrainGrid[0,0],              null, _terrainGrid[2,0], _terrainGrid[1,1]);
    69.         _terrainGrid[1,1].SetNeighbors(_terrainGrid[0,1], _terrainGrid[1,0], _terrainGrid[2,1], _terrainGrid[1,2]);
    70.         _terrainGrid[1,2].SetNeighbors(_terrainGrid[0,2], _terrainGrid[1,1], _terrainGrid[2,2],              null);
    71.         _terrainGrid[2,0].SetNeighbors(_terrainGrid[1,0],              null,              null, _terrainGrid[2,1]);
    72.         _terrainGrid[2,1].SetNeighbors(_terrainGrid[1,1], _terrainGrid[2,0],              null, _terrainGrid[2,2]);
    73.         _terrainGrid[2,2].SetNeighbors(_terrainGrid[1,2], _terrainGrid[2,1],              null,              null);
    74.     }
    75.  
    76.     void Update ()
    77.     {
    78.         Vector3 playerPosition = new Vector3(PlayerObject.transform.position.x, PlayerObject.transform.position.y, PlayerObject.transform.position.z);
    79.         Terrain playerTerrain = null;
    80.         int xOffset = 0;
    81.         int yOffset = 0;
    82.         for (int x = 0; x < 3; x++)
    83.         {
    84.             for (int y = 0; y < 3; y++)
    85.             {
    86.                 if ((playerPosition.x >= _terrainGrid[x,y].transform.position.x) &&
    87.                     (playerPosition.x <= (_terrainGrid[x,y].transform.position.x + _terrainGrid[x,y].terrainData.size.x)) &&
    88.                     (playerPosition.z >= _terrainGrid[x,y].transform.position.z) &&
    89.                     (playerPosition.z <= (_terrainGrid[x,y].transform.position.z + _terrainGrid[x,y].terrainData.size.z)))
    90.                 {
    91.                     playerTerrain = _terrainGrid[x,y];
    92.                     xOffset = 1 - x;
    93.                     yOffset = 1 - y;
    94.                     break;
    95.                 }
    96.             }
    97.             if (playerTerrain != null)
    98.                 break;
    99.         }
    100.      
    101.         if (playerTerrain != _terrainGrid[1,1])
    102.         {
    103.             Terrain[,] newTerrainGrid = new Terrain[3,3];
    104.             for (int x = 0; x < 3; x++)
    105.                 for (int y = 0; y < 3; y++)
    106.                 {
    107.                     int newX = x + xOffset;
    108.                     if (newX < 0)
    109.                         newX = 2;
    110.                     else if (newX > 2)
    111.                         newX = 0;
    112.                     int newY = y + yOffset;
    113.                     if (newY < 0)
    114.                         newY = 2;
    115.                     else if (newY > 2)
    116.                         newY = 0;
    117.                     newTerrainGrid[newX, newY] = _terrainGrid[x,y];
    118.                 }
    119.             _terrainGrid = newTerrainGrid;
    120.             UpdateTerrainPositionsAndNeighbors();
    121.         }
    122.     }
    123. }
    124.  
    can i upload the zip file then someone help me make it work... and yes, i know i'm asking a lot:(
     
    Last edited: Mar 1, 2015
  28. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    My code works for me, but it only generates swamp and rainforest. And its slow. Tweaking, will be back with a working project for you.
     
  29. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This is kind of what the output looks like.

    Its also worth doing up a map like this. This just shows straight humidity increasing as you go right, and temperature increasing as you go up. Get this map right first, and your generated terrain will feel much more natural.

    And here is the Unity package. Note that its incredibly slow, instantiating 10,000 objects is not child's play. There are dozens of better ways to manage the output. Like writing pixels to a texture on a quad. Or using a particle system. But it does work after a fashion.
     
  30. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    This is very interesting and works well:)... just a bit slow like you said.
    Is there any way i can apply this to a physical terrain instead of many blocks?
    basically attach this script to the terrain then on the terrain it does what it does on the many blocks(no more blocks generating = less time??)?
     
  31. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I've been tweaking and applied it directly to a texture. Works well enough. And the entire thing generates with no noticeable lag. I see no reason why it can't be applied directly to terrain.