• 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
Question by aclee · Sep 30, 2014 at 07:15 PM · javascriptterrain

Cube terrain with perlin noise

hi, iv made a script (JS) that generate a 20x20 terrain of cubes, with random hills using perlin noise. alt text

but i have a few problems to solve and questions to ask:

when i generate the terrain again it gives me the exact same terrain, how would i make it automatically give me something else?

how would i make this infinite? how would i make it so when the player goes forward into the world the world would keep up with him? but still look normal (can i make something with chunks?)

how can i implement a seed system ware when you enter a seed you will get the same terrain every time?

sorry if it's a lot of questions...

the script: var size:Vector2 = new Vector2(20, 20); var height:float = 10.0f; var noiseStrength:float = 10.0f;

 private var perlinNoise;
 
 private var root:GameObject;
 
 function OnGUI(){
     if(GUI.Button (Rect(10, 10, 100, 30), "Generate")){
     Generate();
     }
 }
 
 function PerlinNoise(x:float, y:float){
     var noise:float = Mathf.PerlinNoise(x / noiseStrength, y/ noiseStrength);
     return noise * height;
 }
 
 function Generate(){
     Destroy(GameObject.Find("Terrain"));
     
     root = new GameObject("Terrain");
     root.transform.position = new Vector3(size.x/2, 0, size.y/2);
     
     for(var i:int = 0; i <= size.x; i++){
         for(var p:int = 0; p <= size.y; p++){
             var box:GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
             box.transform.position = new Vector3( i, PerlinNoise( i, p ), p);
             box.transform.parent = root.transform;
         }
     }
     
     root.transform.position = Vector3.zero;
     print(PerlinNoise);
 }
capture.png (31.1 kB)
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by ExtremePowers · Sep 30, 2014 at 07:20 PM

For the result being the same all the time, add a seed to the x and y Like this: var noise:float = Mathf.PerlinNoise((Seed + x) / noiseStrength, (Seed + y) / noiseStrength);

To save the seed try

 var Seed = 0;
 
 function Start() {
     if (PlayerPrefs.HasKey("Seed")) {
         Seed = PlayerPrefs.GetInt("Seed");
     }
 }
 
 function OnGUI() {
     Seed = GUI.TextField(Rect(0, 50, 100,30), Seed.ToString()); 
     if (GUI.Button(Rect(0,0,100,30), "Save seed") {
         PlayerPrefs.SetInt("Seed", Seed);
         PlayerPrefs.Save();
     }
 }
 
 
Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Multiple Cars not working 1 Answer

procedural terrain 1 Answer

Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers

A node in a childnode? 1 Answer

Items with Statistics(such as attack damage) that actually effect the character? 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