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 DoctorMoney · Jun 20, 2014 at 01:54 AM · terrainrandomgenerationheight

Generate Random Terrain on Start

Making a game, need to generate random terrain on every start up.

Shamelessly stole someones code for terrain generation, it's good but I can't find where I got it from but all credit to that guy.

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Collections.Generic;
 
 public class RandomTerrainGenerator : ScriptableWizard {
     
     //The higher the numbers, the more hills/mountains there are
     private float HM = Random.Range(0, 10);
     
     //The lower the numbers in the number range, the higher the hills/mountains will be...
     private float divRange = Random.Range(12, 25);
     
     [MenuItem("Terrain/Generate Random Terrain")]
     public static void CreateWizard(MenuCommand command)
     {
         ScriptableWizard.DisplayWizard("Generate Random Terrain", typeof(RandomTerrainGenerator));
     }
     
     void OnWizardCreate()
     {
         GameObject G = Selection.activeGameObject;
         if (G.GetComponent<Terrain>())
         {
             GenerateTerrain(G.GetComponent<Terrain>(), HM);
         }
     }
     
     //Our Generate Terrain function
     public void GenerateTerrain(Terrain t, float tileSize)
     {
         
         //Heights For Our Hills/Mountains
         float[,] hts = new float[t.terrainData.heightmapWidth, t.terrainData.heightmapHeight];
         for (int i = 0; i < t.terrainData.heightmapWidth; i++)
         {
             for (int k = 0; k < t.terrainData.heightmapHeight; k++)
             {
                 hts[i, k] = Mathf.PerlinNoise(((float)i / (float)t.terrainData.heightmapWidth) * tileSize, ((float)k / (float)t.terrainData.heightmapHeight) * tileSize)/ divRange;
             }
         }
         Debug.LogWarning("DivRange: " + divRange + " , " + "HTiling: " + HM);
         t.terrainData.SetHeights(0, 0, hts);
     }
 }
 

I'm using javascript primarily but what I want to do is use that script to generate terrain in an Awake function from another script. I can't get unity to recognize the script i'm trying to get it from or something because I always get an error at the start no matter what I do.

 var player : GameObject;
 var terrainGenerator : MonoScript; //Someone told me monoscript, not really sure it's right though
 var selfTerrain : Terrain;
 private var terrainHeight : float;
 private var playerx : float;
 private var playerz : float;
 
 function Awake () {
 
 //Generate Terrain
 selfTerrain.terrainData = terrainGenerator.GenerateTerrain();
 
 
 }
 function Start () {
 
 //Set player at terrain height
 playerx = player.transform.position.x;
 playerz = player.transform.position.z;
 terrainHeight = selfTerrain.terrainData.GetHeight(playerx, playerz);
 player.transform.position.y = terrainHeight + 1;
 
 
 }


Any help?

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
1
Best Answer

Answer by jceipek · Jun 20, 2014 at 08:04 AM

In order to call C# code from within Unityscript you need to put it in a folder that gets compiled earlier (see http://forum.unity3d.com/threads/mixing-c-and-javascript-how.23274/). For example, put the C# script in Assets/Plugins (create it if it doesn't exist). That's the main error you were getting. Then, the class of the C# code is what you would typically use. However, that won't work properly in this case because the C# code you started with is intended to run in the editor, not at runtime.

Furthermore, multiple languages in your Unity codebase makes things really confusing, so here's a port of the script you found to UnityScript. I haven't used it for a few years (C# has more compile-time checking so I've been using it to write less buggy code), so I can't claim that this code is clean, but it should be a good starting point and I've tested it by creating a terrain and adding the script to it.

 var selfTerrain : Terrain; 
 
 function Awake () {
     selfTerrain = GetComponent(Terrain);
     GenerateTerrain(100f);
 }
 
 function GenerateTerrain(tileSize: float) {
 
     //The higher the numbers, the more hills/mountains there are
     var HM : float = Random.Range (0, 10);
     //The lower the numbers in the number range, the higher the hills/mountains will be...
     var divRange:float  = Random.Range (12, 25);
 
     var height = selfTerrain.terrainData.heightmapHeight;
     var width = selfTerrain.terrainData.heightmapWidth;
 
     //Heights For Our Hills/Mountains
     var hts : float[,] = new float[width,height];
     for (var i :float = 0; i < width; i++) {
         for (var k :float = 0; k < height; k++) {
             hts [i, k] = Mathf.PerlinNoise ((i / width) * tileSize, (k / height) * tileSize) / divRange;
         }
     }
     Debug.LogWarning ("DivRange: " + divRange + " , " + "HTiling: " + HM);
     selfTerrain.terrainData.SetHeights (0, 0, hts);
 }
Comment
Add comment · Show 1 · 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
avatar image DoctorMoney · Jun 20, 2014 at 04:48 PM 0
Share

I can use that and work it in, thanks a bunch man!

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

Invisible trees? 1 Answer

Tree position on random Terrain (y axis) 0 Answers

How to generate random terrain for a platformer 0 Answers

Unity Voxel Terrain and structures 0 Answers

Tile Terrain Generation 1 Answer


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