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 dabluezpreacher · Feb 15, 2019 at 10:50 PM · rigidbodytreestree creator

tree creator tree chopping question

So I am coming into Unity from Unreal 4. In unreal i had a tree chopping system that I was curious of how to do here. Unreal had a terrain editor where I could put my tree model in and it would paint it to the terrain as a static mesh. The blueprint i made was when I did a line trace (raycast for unity) , i could get close to the tree, left click and it would remove the static tree mesh and simulateansly add the physics enabled tree in its place and fall over. I assumed that the static mesh would be better in large volumes than hundreds of physics enabled meshes as far as performance. From what I can see in unity, the terrain tool also paints via a static mesh type of model with collision. Is this better on performance than putting hundreds of rigidbody tree meshes in by hand? If so, how can I script it so that the tree model I have painted via the terrain tools, can disappear on left click and be replaced at the same time with a rigid body tree?

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

Answer by dan_wipf · Feb 18, 2019 at 09:32 AM

Basically you need to Setup Colliders for a Raycast => if I'm right UnityTerrainCollider will merge all TreeColliders in to one(TerrainCollider), so you won't be able to get quite a specific Information for your Ray. So you have to setup Colliders for each Tree.


fyi => UnityTerrain stores Instances of all the Tree's in an Array TreeInstances[] which hold's some Information (PrototypeIndex(Prefab Information)) Position, Rotation) but you have to do some work to Use TreeInstance / PrototypeIndex.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 using System.Linq;
 
 public class choptree : MonoBehaviour
 {
     Terrain ter;
     TerrainData ted;
     List<GameObject> Colliders;
     void OnEnable(){
 
         ter = Terrain.activeTerrain;
         ted = ter.terrainData;
         Colliders = new List<GameObject>();
 
         int TerrainMultiplier = ted.heightmapWidth * ted.heightmapHeight;
 
 
         // Checkthrough all Instances
         for (int i = 0; i<ted.treeInstances.Length;i++){
             GameObject tree = ted.treePrototypes[ted.treeInstances[i].prototypeIndex].prefab;
             CapsuleCollider tempCol;
             //Get Collider from Original prefab (right now just Capsule Collider)
             if(tree.GetComponent<CapsuleCollider>() != null){
                 tempCol = tree.GetComponent<CapsuleCollider>();
             }if(tree.GetComponentInChildren<CapsuleCollider>() != null){
                 tempCol = tree.GetComponentInChildren<CapsuleCollider>();
             }else{
                 tempCol = new CapsuleCollider();
             }
 
             //Spawn Collider GameObject
 
             GameObject tempGo = new GameObject();
             tempGo.name = i.ToString();
 
             //Get Position of WorldPos
             
             Vector3 worldPos = ted.treeInstances[i].position - ter.transform.position;
             worldPos.x /= ted.size.x;
             worldPos.y /= ted.size.y;
             worldPos.z /= ted.size.z;
             worldPos *= TerrainMultiplier;
             tempGo.transform.position = worldPos;
 
             //Add a Collider to the right Position.
             tempGo.AddComponent<CapsuleCollider>();
             tempGo.GetComponent<CapsuleCollider>().height = tempCol.height;
             tempGo.GetComponent<CapsuleCollider>().radius = tempCol.radius;
             tempGo.GetComponent<CapsuleCollider>().center = tempCol.center;
 
             //CleanUp Hirarchy
             tempGo.transform.parent = ter.transform;
             tempGo.hideFlags = HideFlags.HideInHierarchy;
             Colliders.Add(tempGo);
         }
     }
 
     void Update()
     {
         RaycastHit hit;
         if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit)){
             //Check for Input and if hit is a valid target in CollidersList
             if(Input.GetMouseButtonUp(0) && Colliders.Any(x => x.name == hit.transform.name)){
                 int i = Int32.Parse(hit.transform.name);
                 Debug.Log(i);
                 var instances = ted.treeInstances.ToList();
                 //Get treeIndex for Spawning sameTree.
                 var index = ted.treeInstances[i].prototypeIndex;
                 GameObject FallenTree = Instantiate(ted.treePrototypes[index].prefab,Colliders[i].transform.position,Quaternion.identity,ter.transform);
                 //Remove TreeInstance from Terrain and CollidersList
                 instances.RemoveAt(i);
                 Colliders.RemoveAt(i);
                 //update treeInstance Array from Terrain
                 ted.treeInstances = instances.ToArray();
             }
         }
        
     }
 }
 

  
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

136 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 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 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 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

Terrain Trees Disappear (Tree Distance is 2000) 1 Answer

Tree billboards not displaying correctly. 1 Answer

How to scale up a rigidbody without moving it? 1 Answer

Tree color variation with _TreeInstanceColor not working anymore? 2 Answers

Tree branches as mesh 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