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 acehwk · Jul 07, 2013 at 03:32 PM · raycastcolliderterraintree

How can I separate tree colliders from terrain colliders?

Hi. I'm making a game that has a birdseye view perspective camera that the player can control by scrolling using the screen sides or zooming using the mouse wheel. The camera has an effect that when it is within a certain distance from the ground, it will match the changes in elevation as it scrolls.

My problem is that when I add colliders to my terrain's trees, the camera tries to adjust its height to match the height of each tree that it passes over. I cannot use tag tests to get around this as the trees will always have the same tag as the terrain. I tried to test for collider type, but I either did something wrong or that method isn't possible in this scenario.

I could duplicate my terrain and remove all trees and textures and tag this duplicate as something different and only use it for camera height adjustments, but I'm worried about the effect on performance, and the issues it brings whenever I want to manipulate terrain either in editor or in game. I could also manually add in a separate game object with all of my tree colliders, but this would have similar issues as the other method as well as being pretty time consuming.

So, my question is: What is an easy, low cost way to differentiate between terrain and the trees attached to that terrain when using a raycast from a birdseye camera?


I really don't think code is that relevant here, but just in case...

 GameObject cameraObj;
 bool canZoomIn;
 
 float cameraHeightMin = 40f;
 float cameraHeightFlow = 2f;
 float terrainHeight;
 
 void SetFinalCamHeight()
 {
     Vector2 screenCenter = new Vector2(theScreenWidth / 2, theScreenHeight / 2);
     Ray testRay = cameraObj.camera.ScreenPointToRay(screenCenter);
     RaycastHit testHit;
     
     float cameraHeightFinal;
     float terrainHeightOld = terrainHeight; // storing the value of the camera height modifier from the test done in the previous update
     
     if (Physics.Raycast(testRay, out testHit))
     {
         if (testHit.collider.tag == "Ground" || testHit.collider.tag == "Ocean")
         {
             terrainHeight = testHit.point.y;
             
             // if cam height is less than the min allowed height (adjusted for terrain), sets it to the min allowed hieght, and tells the camera that it can no longer zoom in
             if (cameraObj.transform.position.y < (terrainHeight + cameraHeightMin))
             {
                 cameraHeightFinal = terrainHeight + cameraHeightMin - cameraObj.transform.position.y;
                 cameraObj.transform.position += new Vector3(0f, cameraHeightFinal, cameraHeightFinal * -0.5f);
                 // Z value adjusted to account for fact that the camera is at a 60 angle.  i.e., camera must move 1 unit back for each 2 units up in order to maintain its center of focus
                 canZoomIn = false;
             }
             
             // if the camera is above the minimum height from the ground, but still somewhat close, it should raise and lower with the terrain for visual effect
             else if (cameraObj.transform.position.y < (terrainHeight + (cameraHeightFlow * cameraHeightMin)))
             {
                 cameraHeightFinal = terrainHeight - terrainHeightOld;
                 cameraObj.transform.position += new Vector3(0f, cameraHeightFinal, cameraHeightFinal * -0.5f);
             }
         }
     }
 }
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
0

Answer by drift501 · Jul 08, 2013 at 05:51 AM

There may be som way to hack but the only way I know how would be to create a tree-less terrain and then manually place trees by hand. This is the only way I know how. It takes longer but you have more control over each tree and they are seperated from the terrain collider

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 getyour411 · Feb 26, 2014 at 09:32 AM

http://answers.unity3d.com/questions/650308/how-do-i-interact-with-terrain-trees.html

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

19 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

Related Questions

Getting treePrototype information from RayCasting collisions onto terrain? 2 Answers

Disabling colliders in Terrain Trees? 1 Answer

Tree Colliders not working 1 Answer

Colliding with trees using mesh colliders 1 Answer

CreateTerrainGameObject not creating tree colliders? 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