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
1
Question by maikkel · Oct 05, 2010 at 09:54 PM · terrainsetheights

please explain Terrain.SetHeights

I'm trying to set terrain heights by code but i just don't get the Terrain.SetHeights command. Can someone please explain it so even a noob can understand it (best with example)? There are NO examples in the code reference...

How can I create the needed attribute "float[,]"?? Is it even possible via javaScript? I read somewhere that this can only be done in C#, but I thought that the programming language doesn't matter in Unity...

I could get the current heights via Terrain.GetHeights. How can I now edit these settings?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by maikkel · Oct 06, 2010 at 11:17 AM

I got it now, Here's a test script to randomize heights and reset them, with buttons. Attach the script to the Terrain:

var terrain : Terrain; var tData : TerrainData;

var xRes : int; var yRes : int;

var heights : float[];

function Start () { terrain = transform.GetComponent(Terrain); tData = terrain.terrainData;

 xRes = tData.heightmapWidth;
 yRes = tData.heightmapHeight;

 terrain.activeTerrain.heightmapMaximumLOD = 0;

}

function OnGUI() {

 if(GUI.Button (Rect (10, 10, 100, 25), "Wrinkle")) {
     randomizePoints(0.1);
 } 

 if(GUI.Button (Rect (10, 40, 100, 25), "Reset")) {
     resetPoints();
 } 

}

function randomizePoints(strength:float) { var heights = tData.GetHeights(0, 0, xRes, yRes);

 for (y = 0; y < yRes; y++) {
     for (x = 0; x < xRes; x++) {
         heights[x,y] = Random.Range(0.0, strength) * .5; 
     }
 }

 tData.SetHeights(0, 0, heights);

}

function resetPoints() { var heights = tData.GetHeights(0, 0, xRes, yRes);

 for (y = 0; y < yRes; y++) {
     for (x = 0; x < xRes; x++) {
         heights[x,y] = 0; 
     }
 }

 tData.SetHeights(0, 0, heights);

}

Comment
Add comment · 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
2
Best Answer

Answer by Mike 3 · Oct 05, 2010 at 11:44 PM

it's possible, but you need a c# script to create the 2d array in the first place

What you can do is grab the MultiDimArray script from the wiki ( http://www.unifycommunity.com/wiki/index.php?title=JavascriptMultiDimArrays ), then fill int he values yourself

The array itself is basically similar to a greyscale texture - you set a 0->1 value for each item in it, and it maps to an x/y coordinate, e.g. yourArray[x,y] = 0.5;

If you just needed to grab the original heights and change them, you'd just use var by itself without a type, e.g. var heights = Terrain.activeTerrain.terrainData.GetHeights(0, 0, 10, 10); then access them as above

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 maikkel · Oct 06, 2010 at 11:10 AM 0
Share

thanks for the tips, i also got some help in the forums, i think I got it now.

avatar image
3

Answer by eurasian · Oct 25, 2014 at 04:25 PM

This is the same code in C#, for those interested:

 using UnityEngine;
 using System.Collections;
 
 public class RandomizeHeights : MonoBehaviour {
 
     Terrain  terrain; 
     TerrainData tData;
     
     int xRes;
     int yRes;
     
     float[,] heights;
     
     void Start () { 
         terrain = transform.GetComponent<Terrain>(); 
         tData = terrain.terrainData;
         
         xRes = tData.heightmapWidth;
         yRes = tData.heightmapHeight;
     }
     
     void OnGUI() {
         if(GUI.Button (new Rect (10, 10, 100, 25), "Wrinkle")) {
             randomizePoints(0.1f);
         }
         
         if(GUI.Button (new Rect (10, 40, 100, 25), "Reset")) {
             resetPoints();
         } 
     }
     
     void randomizePoints(float strength) { 
         heights = tData.GetHeights(0, 0, xRes, yRes);
         
         for (int y = 0; y < yRes; y++) {
             for (int x = 0; x < xRes; x++) {
                 heights[x,y] = Random.Range(0.0f, strength) * 0.5f;
             }
         }
         
         tData.SetHeights(0, 0, heights);
     }
     
     void resetPoints() { var heights = tData.GetHeights(0, 0, xRes, yRes);
         for (int y = 0; y < yRes; y++) {
             for (int x = 0; x < xRes; x++) {
                 heights[x,y] = 0;
             }
         }
         
         tData.SetHeights(0, 0, heights);
     } 
 
     // Update is called once per frame
     void Update () {
     
     }
 }
 

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Why _terrainData.SetHeights() resizes the terrain? 0 Answers

Big FPS drop when using SetHeights on terrain 2 Answers

Very Very Big Terrain, how to show it? 1 Answer

How to use Perlin Noise to generate terrain tiles? 3 Answers

How can I programmatically stretch a Terrain? 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