Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by the_sorrow · Jul 11, 2012 at 08:50 PM · collisionobjectcharactertreesolid

Tree collision

Okay so I'm using a First person Character controller and I'm trying to make the tree's seem solid In-game by making them collide with my Character, now I've got the 'Create tree colliders' option ticked in the inspector pane when my terrain is selected.

I created the tree's using the 'Place trees' button when I had my terrain selected and I painted them on, but my tree's still dont have collision with the Character.

Any help would be thoroughly appreciated as I'm a beginner to Unity, It seems a simple task like this thats been explained to others is one which I still cannot grasp and is the reason I looked to have an answer explained directly to me instead

P.S, They arent tree's I created, they came with unity, The tree name I used was 'Big tree', Thankyou

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

Answer by Loren-Logic · Feb 26, 2013 at 05:45 PM

This is a frequently asked question. The most popular answer is some derivative of the reference manual's topic, "Setting Up Tree Collisions." That works just fine, and potentially gives every tree its own collider.

My cheapo computer has a cheapo video card in it, and I keep it that way so I am forced to optimize my games for those who also have lousy video cards. It occurred to me that my FPS game's flying protagonist can really only hit the nearest tree, so giving every tree a collider means physics processing for every tree but one has no return on investment. Why not move one efficient capsule collider to the nearest tree in case I smack into it? A built-in array and a little math in a short script sped up my FPS dramatically compared to painting trees on my terrain with one probably unused capsule collider each. I got rid of the capsule collider in my prototype tree and supplied the original colliderless tree to the terrain tool for painting onto my terrain. I attached the following script "sctTerrain" to the terrain object named "Terrain." I added a capsule object to the scene having a capsule collider and a kinematic rigidbody (recommended by Unity if I am going to move my collider around a lot). I sized the capsule by hand to appropriately match the prototype tree, the top branches of which I can fly through without dying. The capsule has its mesh renderer turned off so it is invisible but still solid. I named the capsule "Tree" so if I fly into the capsule I can report, "Tree got you." If my script can't find any trees AND cannot find the capsule object, it destroys itself at Start() because it is unneeded. Otherwise it places the capsule to surround the nearest tree trunk and scales it to match the tree's height less the sparse top branches. This math-based technique for colliding with trees is fast, and I recommend it:

 #pragma strict
 #pragma downcast
 
 private var paryTrees : TreeInstance[];
 private var pvecTerrainPosition : Vector3;
 private var pvecTerrainSize : Vector3;
 private var pgobTreeCollide : GameObject;
 private var pvecCollideScale : Vector3;
 private var pbooCollideWithTrees : boolean = false;
 
 function Start(){
 
     // Get the terrain's position
     pvecTerrainPosition = Terrain.activeTerrain.transform.position;
 
     // Get the terrain's size from the terrain data
     pvecTerrainSize = Terrain.activeTerrain.terrainData.size;
     // Get the tree instances
     paryTrees = Terrain.activeTerrain.terrainData.treeInstances;
     
     // Get the invisible capsule having the capsule collider that makes the nearest tree solid
     pgobTreeCollide = GameObject.Find("Tree");        // This is a capsule having a capsule collider, but when the flier hits it we want it to be reported that the flier hit a tree.
     
     // Are there trees and a tree collider?
     if ((pgobTreeCollide != null) && (paryTrees.length > 0)){
         // Set a flag to make this script useful
         pbooCollideWithTrees = true;
         // Get the original local scale of the capsule.  This is manually matched to the scale of the prototype of the tree.
         pvecCollideScale = pgobTreeCollide.transform.localScale;
     }
     // No need to use this script
     else {Destroy(this);}
 }
 
 function Update () {
     var L : int;
     var triTree : TreeInstance;
     var vecFlier : Vector3 = sctFly.svecXYZ;        // My protagonist's position, passed by a static variable in a script called sctFly.
     var fltProximity : float;
     var fltNearest : float = 9999.9999;                // Farther, to start, than is possible in my game.
     var vec3 : Vector3;
     var vecTree : Vector3;
     var intNearestPntr : int;
     
     // Test the flag
     if (pbooCollideWithTrees == true){
         // Find the nearest tree to the flier
         for (L = 0; L < paryTrees.length; L++){
             // Get the tree instance
             triTree = paryTrees[L];
             // Get the normalized tree position
             vecTree = triTree.position;
             // Get the world coordinates of the tree position
             vec3 = (Vector3.Scale(pvecTerrainSize, vecTree) + pvecTerrainPosition);
             // Calculate the proximity
             fltProximity = Vector3.Distance(vecFlier, vec3);
             // Nearest so far?
             if (fltProximity < fltNearest){
                 // Remember the nearest
                 fltNearest = fltProximity;
                 // Remember the index
                 intNearestPntr = L;
             }
         }
         // Get the closest tree
         triTree = paryTrees[intNearestPntr];
         // Get the normalized tree position of the closest tree
         vecTree = triTree.position;
         // Get the world coordinates of the tree position
         vec3 = (Vector3.Scale(pvecTerrainSize, vecTree) + pvecTerrainPosition);
         // Scale the capsule having the capsule collider that represents a solid tree
         pgobTreeCollide.transform.localScale = (pvecCollideScale * triTree.heightScale);
         // Add some height to position the capsule correctly on the tree
         vec3.y += pgobTreeCollide.transform.localScale.y;
         // Position the capsule having the capsule collider at the nearest tree
         pgobTreeCollide.transform.position = vec3;
     }
 }

By the way, speaking of optimization, I've got my "Billboard start" setting for trees at 16, very close. It is more disconcerting to see trees change from billboards to meshes at a distance-- unless it is a huge distance that slows my system down a LOT-- than it is to see flat trees up close. Close-up tree billboards look fine, and the transition to meshes is almost unnoticeable as I fly past. Even from high above them, when you'd think they'd look like foreshortened plane segments, billboard trees look good. Somebody at Unity was thinking! Thanks, Loren

Comment
Add comment · Show 10 · 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 AlucardJay · Feb 27, 2013 at 02:52 AM 0
Share

Awesome answer, upvoted. I just wrote a script that finds all the trees and just adds colliders at every position, but am very keen to test and use this method now. $$anonymous$$any Thanks =]

avatar image pabloromocl · Jun 25, 2013 at 03:13 PM 0
Share

Best answer ever. Thanks this script is awesome.

avatar image Sisso · Jun 25, 2013 at 03:32 PM 3
Share

Did you really profiling it? It is improbably that for each update, for each tree, compute a distance with sqr be fast that an engine that probably use Octrees and Axis Align Bound Box. Your code is linear complexity for trees and expotential for player, where octree is logarithm for both. Your code use Distance with SQR for each check, while a AABB use only simple operations.

avatar image crusherxman · Jul 07, 2013 at 08:58 PM 0
Share

Excellent answer! I really love it :)

avatar image nastasache · Sep 21, 2013 at 08:42 AM 0
Share

Brilliant idea!

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

13 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

Related Questions

Tree collision 1 Answer

How can i make my scene restart once my character has collided with another object? 2 Answers

Detonation of an object on collision of character.... 1 Answer

Raycast hit in unity 3d 1 Answer

Why can't my character jump 2 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