Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Neolith998 · Feb 13, 2016 at 03:55 PM · terraingrassterraindata

Placing grass on terrain in script on a certain height

I'd like my grass to be placed only on certain heights of the terrain (so grass wouldn't be placed underwater or high up in the mountains). I know there is a way to do this, I just can't really wrap my head around on how to edit the grass. Could anyone help me out here? Here is my code which places grass all over the terrain.

 void Start()
     {
         terrainToPopulate = transform.gameObject.GetComponent<Terrain>();
         terrainToPopulate.terrainData.SetDetailResolution(grassDensity, patchDetail);
 
         int[,] newMap = new int[grassDensity, grassDensity];
 
         for (int i = 0; i < grassDensity; i++)
         {
             for (int j = 0; j < grassDensity; j++)
             {
                 newMap[i, j] = 6;
             }
         }
         terrainToPopulate.terrainData.SetDetailLayer(0, 0, 0, newMap);
     }

Thank you in advance.

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

2 Replies

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

Answer by Winterblood · Feb 13, 2016 at 08:19 PM

Try this, then customise the height/density rules as you need them:

 void Start()
 {
     terrainToPopulate = transform.gameObject.GetComponent<Terrain>();
     terrainToPopulate.terrainData.SetDetailResolution(grassDensity, patchDetail);
  
     int[,] newMap = new int[grassDensity, grassDensity];
  
     for (int i = 0; i < grassDensity; i++)
     {
         for (int j = 0; j < grassDensity; j++)
         {
             // Sample the height at this location (note GetHeight expects int coordinates corresponding to locations in the heightmap array)
             float height = terrainToPopulate.terrainData.GetHeight( i, j );
             if (height < 10.0f)
             {
                 newMap[i, j] = 6;
             }
             else
             {
                 newMap[i, j] = 0;
             }
         }
     }
     terrainToPopulate.terrainData.SetDetailLayer(0, 0, 0, newMap);
 }
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 Neolith998 · Feb 13, 2016 at 09:57 PM 0
Share

Thank you, it worked when I switched .GetHeight( i, j ); to .GetHeight( j, i );.

avatar image
0

Answer by manyokibenc · Feb 14, 2016 at 05:51 PM

Hello, my grass creator script works by checking the terrain's texture data, and places grass only to specific textures, so you should use Terrain Toolkit to apply textures by height, and use a code like mine to generate the grass. I will put my script to asset store, so if you are interested please contact me! Cheers B.

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

36 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Save terrain grass data 0 Answers

Gras Dissapears in build 1 Answer

Terrain heightmap isn't smooth 0 Answers

Unity terrain grass extremely weird 0 Answers

Plant grass or tree with an image 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