Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by kirby4tw · Nov 23, 2015 at 02:35 AM · c#scripting problemcolliderterraintrees

How to destroy Terrain-Tree-Collider?

Hey guys!

I am using a chop-wood-from-terrain-trees-script. All working fine but there is on thing i dont get. After a tree is destroyed, the collider still exist (invisible). Any ideas? :(

 public class TreesManager : MonoBehaviour
 {
     public int TreeHP = 200;
     public int DropNum = 10;
     public float DropArea = 3;
     public Terrain terrain;
     public float BlastArea = 0.5f;
     public GameObject WoodDrop;
     public float ResetTime = 600;
 
     private List<int> RemovedIndexList = new List<int>();
     private SceneManager sceneManage;
     private List<Logger> Loggers = new List<Logger>();
     private TreeInstance[] treesBackup;
     private int ChopIndex;
     private bool hited = false;
     private float timeTemp;
 
 
     void Start()
     {
         sceneManage = (SceneManager)GameObject.FindObjectOfType(typeof(SceneManager));
         if (sceneManage)
         {
             if (Network.isClient)
             {
                 sceneManage.GetInitializeeScene();
             }
         }
 
         if (terrain == null)
             terrain = this.GetComponent<Terrain>();
 
         timeTemp = Time.time;
         BackupTree();
     }
 
     void Update()
     {
         hited = false;
         if (Time.time >= ResetTime + timeTemp)
         {
             if (Network.isServer || Network.isClient)
             {
                 if (Network.isServer)
                 {
                     if (sceneManage)
                         sceneManage.ResetAllTrees();
                 }
             }
             else
             {
                 ResetTrees();
             }
             timeTemp = Time.time;
         }
     }
 
     public void ResetTrees()
     {
         RemovedIndexList.Clear();
         Loggers.Clear();
         terrain.terrainData.treeInstances = treesBackup;
     }
 
     bool LoggerChecker(int index)
     {
         // we cannot put HP variable to every trees in the scene
         // so we have to use something to define it by Index of a tree
         // a Logger using for define a HP to each trees
 
         foreach (Logger logger in Loggers)
         {
             if (logger.index == index)
                 return false;
         }
         Logger newLogger = new Logger();
         newLogger.index = index;
         newLogger.HP = TreeHP;
         Loggers.Add(newLogger);
         return true;
     }
 
     bool LoggerApplyDamage(int damage, int index)
     {
         // apply damage to a tree
         if (!LoggerChecker(index))
         {
             foreach (Logger logger in Loggers)
             {
                 if (logger.index == index)
                 {
                     logger.HP -= damage;
                     if (logger.HP <= 0)
                     {
                         return true;
                     }
                 }
             }
         }
         return false;
     }
 
     public void Cuttree(Vector3 position, int damage)
     {
         if (terrain == null)
             return;
 
         // we cannot specific a tree, we just find possibility around the position 
         int index = 0;
         foreach (TreeInstance tree in terrain.terrainData.treeInstances)
         {
             Vector3 treepos = Vector3.Scale(tree.position, terrain.terrainData.size) + Terrain.activeTerrain.transform.position;
             var distance = Vector3.Distance(new Vector3(treepos.x, position.y, treepos.z), position);
             // hit a tree if in ranged
             if (distance < BlastArea)
             {
                 if (LoggerApplyDamage(damage, index))
                 {
                     Drop(position);
                     RemovedIndexList.Add(index);
                     SendARemovedTree(index);
                     return;
                 }
             }
             index++;
         }
 
     }
 
     public void SendARemovedTree(int index)
     {
         if (!Network.isServer && !Network.isClient)
         {
             // in single player
             RemoveATrees(index);
         }
         else
         {
             if (Network.isServer)
             {
 
                 RemoveATrees(index);
 
                 // only server can send a removed tree index;
                 if (sceneManage)
                     sceneManage.SendRemovedTreeIndex(index);
             }
         }
     }
 
     public string GetRemovedTrees()
     {
         // convert array to string using for send via RPC network
         string arraylist = "";
         foreach (int index in RemovedIndexList)
         {
             arraylist += index + ",";
         }
         return arraylist;
     }
 
     public void UpdateRemovedTrees(string indexremoved)
     {
         // get all indexes of removed trees as a string
         if (terrain == null)
             return;
 
         RemovedIndexList.Clear();
 
         // convert them to array
         string[] indexes = indexremoved.Split(","[0]);
         for (int i = 0; i < indexes.Length; i++)
         {
             int res = -1;
             if (int.TryParse(indexes[i], out res))
                 RemovedIndexList.Add(res);
         }
         // save them to a removed index list
 
         List<TreeInstance> instancesTmp = new List<TreeInstance>(terrain.terrainData.treeInstances);
         foreach (int removed in RemovedIndexList)
         {
             // loop a removed index list find and remove a tree
             instancesTmp.RemoveAt(removed);
         }
         // replace a tree instances in terrain data with new array
         terrain.terrainData.treeInstances = instancesTmp.ToArray();
     }
 
     public void RemoveATrees(int index)
     {
         // remove a specific tree by index
         List<TreeInstance> instancesTmp = new List<TreeInstance>();
         int i = 0;
         foreach (TreeInstance tree in terrain.terrainData.treeInstances)
         {
             // loop a all of a tree on terrain
             if (i != index)
             {
                 // basically add every tree to a Tmp array, Except a tree with a specfic index
                 instancesTmp.Add(tree);
             }
             i++;
         }
         // replace a tree instances in terrain data with a Tmp array
         terrain.terrainData.treeInstances = instancesTmp.ToArray();
     }
 
     public void OnHit(DamagePackage dm)
     {
         if (!hited)
         {
             if (Network.isServer || (!Network.isClient && !Network.isServer))
             {
                 Cuttree(dm.Position, dm.Damage);
                 hited = true;
             }
         }
     }
 
     public void Drop(Vector3 position)
     {
         // drap a wood
         if (WoodDrop == null)
             return;
 
         for (int i = 0; i < DropNum; i++)
         {
             if (Network.isClient || Network.isServer)
             {
                 if (Network.isServer)
                     Network.Instantiate(WoodDrop, DetectGround(position + new Vector3(Random.Range(-DropArea, DropArea), 0, Random.Range(-DropArea, DropArea))), WoodDrop.transform.rotation, 2);
             }
             else
             {
                 GameObject.Instantiate(WoodDrop, DetectGround(position + new Vector3(Random.Range(-DropArea, DropArea), 0, Random.Range(-DropArea, DropArea))), WoodDrop.transform.rotation);
             }
         }
     }
 
     Vector3 DetectGround(Vector3 position)
     {
         RaycastHit hit;
         if (Physics.Raycast(position, -Vector3.up, out hit, 1000.0f))
         {
             return hit.point;
         }
         return position;
     }
 
     void BackupTree()
     {
         if (terrain == null)
             return;
         // create backup trees data/
         treesBackup = terrain.terrainData.treeInstances;
     }
 
     void OnApplicationQuit()
     {
         // Importance!! Need to restore all tree before close
         // if not, all the removed trees will be lose forever
         if (terrain == null)
             return;
         terrain.terrainData.treeInstances = treesBackup;
     }
 
 
 }
 
 public class Logger
 {
     public int index;
     public int HP;
 }
 
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
0

Answer by kirby4tw · Nov 23, 2015 at 09:20 AM

Doesnt work because the "tree" / index isnt a GameObject.

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
0

Answer by say_forever · Nov 23, 2015 at 12:27 PM

 Destroy(GetComponent<Terrain>()); 
 Destroy(GetComponent<TerrainCollider>());

hope this is what you need

Comment
Add comment · Show 2 · 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 kirby4tw · Nov 25, 2015 at 02:50 PM 0
Share

hey thank you, but where i can put this code?

avatar image say_forever kirby4tw · Nov 26, 2015 at 01:58 AM 0
Share

It depends on where you what to destroy those component.

avatar image
0

Answer by Reefer · May 23, 2016 at 04:55 PM

 public void RemoveATrees (int index)
     {
         // remove a specific tree by index
         List<TreeInstance> instancesTmp = new List<TreeInstance> ();
         int i = 0;
         foreach (TreeInstance tree in terrain.terrainData.treeInstances) {
             // loop a all of a tree on terrain
             if (i != index) {
                 // basically add every tree to a Tmp array, Except a tree with a specfic index
                 instancesTmp.Add (tree);
             }
             i++;
         }
         // replace a tree instances in terrain data with a Tmp array
         terrain.terrainData.treeInstances = instancesTmp.ToArray ();
         // refresh the terrain, get rid of collider of that tree.
         float[,] heights = terrain.terrainData.GetHeights(0, 0, 0, 0);
         terrain.terrainData.SetHeights(0, 0, heights);
 
     }


I don't know if you need that anymore but change your RemoveATrees to that and it works.

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

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

Related Questions

GameObject not colliding properly with a collider! 3 Answers

OnTriggerExit does not work 0 Answers

When raycasting, script doesn't detect anything coming into ray yet I did everything right! 1 Answer

How to move an object on a terrain that will always stay on top of the terrain? 2 Answers

ui Text not responding to collider 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