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 Prefab · Jan 29, 2016 at 08:58 AM · terrainruntimedisabletreegrass

How to hide (not remove) terrain trees and grass at runtime?

I am using a great little script that will destroy terrain trees within an explosion and instantiate a dead replacement:

 public class TreeExplosion : MonoBehaviour {
 
     public float BlastRange = 10.0f;
     public float BlastForce = 2000.0f;
     public GameObject DeadReplace;
     public GameObject Explosion;
     
     void Explode() {
         Instantiate(Explosion, transform.position, Quaternion.identity);
         TerrainData terrain = Terrain.activeTerrain.terrainData;
         
         ArrayList instances = new ArrayList();
         
         foreach (TreeInstance tree in terrain.treeInstances) {
             float distance = Vector3.Distance(Vector3.Scale(tree.position, terrain.size) + Terrain.activeTerrain.transform.position, transform.position);
             if (distance<BlastRange) {
                 // the tree is in range - destroy it
                 GameObject dead = Instantiate(DeadReplace, Vector3.Scale(tree.position, terrain.size) + Terrain.activeTerrain.transform.position, Quaternion.identity) as GameObject;
                 dead.GetComponent<Rigidbody>().maxAngularVelocity = 1;
                 dead.GetComponent<Rigidbody>().AddExplosionForce(BlastForce, transform.position, BlastRange*5, 0.0f);
             } else {
                 // tree is out of range - keep it
                 instances.Add(tree);
                 
             }
         }
         //Delete original tree
         terrain.treeInstances = (TreeInstance[])instances.ToArray(typeof(TreeInstance));
 
     }
 
     void Update() {
         if (Input.GetButtonDown("Fire1")) {
             Explode();
         }
     }
 }

There are two issues with this script which I cannot work out.

1) Is it possible to only hide/disable a terrain tree temporarily? This is because otherwise, if Player A "goes on a rampage" and destroys all trees in the game level; when Player B comes along to play the same level, there won't be any trees left for them to destroy! It would be better to be able to hide/disable any trees within the explosion, and then on Awake simply unhide/enable all terrain trees. This way any player will be able to "go on a rampage".

2) Using the TerrainData class, it is possible to access individual tree instances of a terrain. However is it also possible to access grass in this way? The reason is that I would like to be able to destroy both the trees and grass (at the moment it is only trees), but I am not quite sure how terrain grass is accessed via script. Can it be done using the TerrainData class, or in some other way?

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 ForbiddenSoul · Jan 29, 2016 at 09:28 AM

  1. If you just want to ensure that someone else gets to blow up the same trees you can save the TreeInstances data into memory, and then 1) have a new player load it back up again when the join the same game, or 2) have it saved back into the terrain file or 3) both.

    using UnityEngine; using System.Collections;

      TreeInstance[] originalTreeInstances;
     
         void Start()
         {
             // Copy the treeInstances, so that we can reload them to the original state later
             originalTreeInstances = Terrain.activeTerrain.terrainData.treeInstances;
         }
     
         void WhenYouWantNewTrees()
         {
             Terrain.activeTerrain.terrainData.treeInstances = originalTreeInstances;
         }
     
         void OnApplicationQuit()
         {
             // restore original trees as you tell the game to close
             Terrain.activeTerrain.terrainData.treeInstances = originalTreeInstances;
         }
    
    
  2. Yes you can access the grass in a similar way, the grass is called Detail. Look here and play around with the stuff named detailWhatever. TerrainData.detailPrototypes for example can tell you what kind of grass it is.

Side Note: That script does not modify the TerrainCollider at all. It's one collider for all your mountains hills tress what ever. If you need to mess with that you have 2 options. Recalculate the Terrain Collier See Here or Disable Terrain Tree colliers from being added to the terrain, and create your own. See Here

I've been doing nothing but screw around with this for the past 3 days, so If you need some help, feel free to message me, it's still fresh in my mind...

EDIT I'm working on an entire Terrain Extension Method that lets you do some cool stuff, it's not finished yet, but if someone wants a copy, leave a comment and I'll upload the most recent version.

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 Prefab · Jan 29, 2016 at 12:19 PM 0
Share

Some great information there. Thanks for the insight.

I haven't yet decided exactly how the terrain data will be stored in the game, so for the time being I was going to simply enable/disable the tree gameobject or turn of its renderer and collider components. Is it possible to use getcomponent to access one of the terrain trees and modify some of its components in this way?

For the grass, I suppose I could use something like detailHeight to make the grass really small, or use detailPrototypes to change the texture to a blank transparent one; either of these would make the grass "disappear" I guess. I haven't played around with these parameters before so I will try it out.

Oh and yes I am not changing the terrain collider in this script, there is another script for that. Sounds like you are working on a great asset, I look forward to seeing it on the Asset Store.

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

33 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

Related Questions

Can Terrain Placed objects play certain sounds? 1 Answer

How to toggle 3D terrain grass rendering on/off? 0 Answers

Plant grass or tree with an image 0 Answers

Hide Grass and Trees applied to terrain? 2 Answers

My grass looks really weird. 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