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 keinabel · Jan 31, 2013 at 03:37 PM · terraintreeremoveplacement

Why is my Tree Removal so low-performing when placing a GameObject nearby

Hi

In my City Builder I have a lonely Island which is not urbanized at all. Which means there are lots of trees. When placing a building on the ground I want the trees to be removed. I actually found out a way to do this. The problems:

  1. It will only work with the first building

  2. Unity freezes while doing it (and while not doint it on the follow-up buildings)

Here's my code:

 foreach (TreeInstance tree in Terrain.activeTerrain.terrainData.treeInstances)
         {
             distance = Vector3.Distance(
                             Vector3.Scale(tree.position
                                       , Terrain.activeTerrain.terrainData.size)
                             + Terrain.activeTerrain.transform.position
                             , _temporaryBuilding.transform.position);
 
             if (distance > 25f)
             {
                 //Terrain.Destroy(tree); TODO is there a way to use destroy?
                 treeInstances.Add(tree); // treeInstances is an ArrayList declared earlier
             }
             Terrain.activeTerrain.terrainData.treeInstances 
                        = (TreeInstance[])treeInstances
                           .ToArray(typeof(TreeInstance));
         }

I can see, that this solution is quite low-performing. Because I always have to add every single tree again. Or at least I guess this might be the reason. But actually I've seen games working just fine with that solution...

But mine is freezing AND the removal won't work on a second try. Only for the first building.

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

Answer by keinabel · Mar 05, 2013 at 05:53 PM

The problem was
Terrain.activeTerrain.terrainData.treeInstances = (TreeInstance[])treeInstances .ToArray(typeof(TreeInstance)); which I accidentically put inside the foreach loop.

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
1

Answer by deltamish · Jan 31, 2013 at 04:18 PM

HI,first try places a group of trees directly as prefab and try removing it using GameObject.SetActive = false;(dont use Destroy Destroying an object calls GarbageCollector which takes a bit of high memmory.This may the cause unity to freeze if there a lot to destory and has only little memmory
) If you want an Script that uses GameObject.SetActive = false; please do feel free to ask

Update Hi here is the sript for the mouse This script is for birdeye view(Top view)

 //omitted Using Unity Engine..etc
 
 public class BuildingPlacer : MonoBehaviour {
 public Transform BuildingA;
 public Transform BuildingB;
 private Transform tgtbuilding;
 int treelayer;
 void Update(
 treelayer = 1<<9 ;` /// -->9 Number of the Tree layer 

 treelayer = ~treelayer;
 if(Input.GetKeyDown(KeyCode.A)){
 tgtbuilding = BuildingA;
 }
 if(Input.GetKeyDown(KeyCode.B)){
 tgtbuilding = BuildingB;
 }
 if(tgtbuilding){
 if(Input.GetButton("Fire1")){
 Ray Pos= Camera.main.ScreenPointToRay(Input.mousePosition);///Note your Camera should be Tagged MainCamera
  
 RaycastHit hit = hit;
 if(Physics.Raycast(Pos,out hit,10000,treelayer) && hit.normal.y > 0.999){
 if(hit.collider.tag == "PlaceableArea"){
 hit.collider.SendMessage("RemoveTrees",SendMesageOptions.DontRequireReceiver);
 PlaceBuilding(tgtbuilding,5,hit.point);
 }
 }
 }else{
 Debug.Log("Select the building by pressing A or B");
 }
 }
 IEnumerator PlaceBuilding(Transform obj,float wait,Vector3 pos2){
 yield WaitForSeconds(wait);
 Instantiate(obj,pos2,Quaternion.FromToRotation(Vector3.up,hit.normal);///for Uneven terrain use Quaternion.FromToRotation(Vector3.up,hit.normal) else use obj.rotation
 }
 }
 }

now Create a box and scale on y axis until it looks like a plane and then tag it PlaceableArea now place Trees overthem manually (Like you place character i.e drag and drop) and create a new layer(le this be on number 9) name them treelayer(or anything you want) and mark the trees and anyobject other than the box tagged PlaceableArea as treelayer

attach this script to the box

 //omitted using unity engine...
 public class TreeRemover(){
 public GameObject[] Trees;//Assign trees place above the box here
 void RemoveTrees(){}
 foreach(int k =0;k<Trees.length;k++){
 Trees[k].SetActive = false;
 }

 }
 }

Hope it works the way you wanted

Comment
Add comment · Show 12 · 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 deltamish · Jan 31, 2013 at 04:32 PM 1
Share

Sorry for my misconception.What i meant was to place trees manually(not using terrain engine) like when you drag an object(lets say an character) from the project panel into the scene view.Hope you got what i am trying to say here

avatar image deltamish · Feb 01, 2013 at 02:16 AM 1
Share

There are several ways in which you can achieve this One of them is using raycast .you can also do this by using angle calculation ,and also using collision detection.Tell me which method you shall use and i shall write the script

avatar image deltamish · Feb 02, 2013 at 05:16 AM 1
Share

By angle calculation means you should calculate the angle between each tree and the y axis of mouse pointer in that way you can get which trees are directly below it.now before i write the script i need to ask you a question

1.do all buildings have same area(same length and breadth)

avatar image deltamish · Feb 06, 2013 at 01:25 PM 1
Share

hi @keinabel can you tell me wether all buildings have same area(same length and breadth) or not so that i can write the script for you

avatar image deltamish · Feb 06, 2013 at 04:30 PM 1
Share

Very well That's good I shall write the script tomorrow cause i am kinda busy with my studies at the movement.

If you wnat the script today itself then i can give you a rough idea of the simple system you are going to use

Just Raycast downwards (-Vector3.up) and see if it hits a plane tagged "Something like area " then if you want to place a building there then send message to the parent of the object or the object itself asking ti remove the trees which is child of the plane

Hope you understand the concept

Reply

Show more comments

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

10 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

Related Questions

Terrain Tree Placement 0 Answers

Remove Trees during runtime 1 Answer

Remove trees dynamically - stuck with collider 3 Answers

Placing tree objects with Terrain or Polybrush which one is useful ?? 0 Answers

Disabling colliders in Terrain Trees? 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