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 Videoman · Oct 07, 2014 at 04:13 AM · terrainterraindataterraincollider

Object2Terrain Creates a Invalid Collider. Help please.

I recently tried to use the Object2Terrain script to convert a FBX mesh to a Terrain Object type. What I have run into is the fact the Object2Terrain does this, but also creates a invalid Terrain Collider. I mean invalid in terms that is does not create a TerrainData object for the Terrain Collider to use.

This not only throws up an error, but it also prevents the usage of the Painting Tool set that is part of the Terrain Object type. Meaning you can't do anything with the Object2Terrain converted object. This in the end makes the script useless and rather pointless. Does anyone know either: a) How to fix this? or b) Another way to convert meshes to Terrain objects?

And yes I have tried creating a new terrain from the drop down menu and then using its Terrain Data, but this actually de-sync's the brush from the cursor and does not allow painting.

Thank you for any help given.

Comment
Add comment · Show 1
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 Videoman · Oct 11, 2014 at 04:38 PM 0
Share

Does anyone know what is going on?

There still has not been anything posted about this issue and how to solve it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mherndon6 · Oct 08, 2016 at 07:32 AM

Hey there. I was having the same issue as you today and stumbled upon your post. My Terrain object had its data somewhere, but the actual asset didn't exist so the Terrain Collider wouldn't work properly. I know it's two years late, but here's a little workaround.

If you open up Object2Terrain.cs, you can see it create the Terrain Data object, but it never gets added as an asset. You can do it yourself with the function call AssetDatabase.CreateAsset (terrain, path). Just make sure the path is relative to your project. Then you just use the tool again and the terrain data will show up in your assets. You can drag that into the terrain collider data field. My new CreateTerrain function looks like this:

     void CreateTerrain(){    
  
         //fire up the progress bar
         ShowProgressBar(1, 100);
  
         TerrainData terrain = new TerrainData();
         terrain.heightmapResolution = resolution;
         GameObject terrainObject = Terrain.CreateTerrainGameObject(terrain);
  
         Undo.RegisterCreatedObjectUndo(terrainObject, "Object to Terrain");
  
         MeshCollider collider = Selection.activeGameObject.GetComponent<MeshCollider>();
         CleanUp cleanUp = null;
  
         //Add a collider to our source object if it does not exist.
         //Otherwise raycasting doesn't work.
         if(!collider){
  
             collider = Selection.activeGameObject.AddComponent<MeshCollider>();
             cleanUp = () => DestroyImmediate(collider);
         }
  
         Bounds bounds = collider.bounds;    
         float sizeFactor = collider.bounds.size.y / (collider.bounds.size.y + addTerrain.y);
         terrain.size = collider.bounds.size + addTerrain;
         bounds.size = new Vector3(terrain.size.x, collider.bounds.size.y, terrain.size.z);
  
         // Do raycasting samples over the object to see what terrain heights should be
         float[,] heights = new float[terrain.heightmapWidth, terrain.heightmapHeight];    
         Ray ray = new Ray(new Vector3(bounds.min.x, bounds.max.y + bounds.size.y, bounds.min.z), -Vector3.up);
         RaycastHit hit = new RaycastHit();
         float meshHeightInverse = 1 / bounds.size.y;
         Vector3 rayOrigin = ray.origin;
  
         int maxHeight = heights.GetLength(0);
         int maxLength = heights.GetLength(1);
  
         Vector2 stepXZ = new Vector2(bounds.size.x / maxLength, bounds.size.z / maxHeight);
  
         for(int zCount = 0; zCount < maxHeight; zCount++){
  
             ShowProgressBar(zCount, maxHeight);
  
             for(int xCount = 0; xCount < maxLength; xCount++){
  
                 float height = 0.0f;
  
                 if(collider.Raycast(ray, out hit, bounds.size.y * 3)){
  
                     height = (hit.point.y - bounds.min.y) * meshHeightInverse;
                     height += shiftHeight;
  
                     //bottom up
                     if(bottomTopRadioSelected == 0){
  
                         height *= sizeFactor;
                     }
  
                     //clamp
                     if(height < 0){
  
                         height = 0;
                     }
                 }
  
                 heights[zCount, xCount] = height;
                    rayOrigin.x += stepXZ[0];
                    ray.origin = rayOrigin;
             }
  
             rayOrigin.z += stepXZ[1];
               rayOrigin.x = bounds.min.x;
               ray.origin = rayOrigin;
         }
  
         terrain.SetHeights(0, 0, heights);

             // Create asset file here
         AssetDatabase.CreateAsset (terrain, "Assets/terrainData.asset");
 
         EditorUtility.ClearProgressBar();
  
         if(cleanUp != null){
  
             cleanUp();    
         }
     }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Possible to make custom editor script to set terrain height? 1 Answer

How can i get terrain data from Terrain.asset? 0 Answers

[SOLVED] Unity 5 - Runtime Terrain Deformation Collider Bug 4 Answers

Unity tree colliders acting very strange 0 Answers

Creating tile grid overlays on area (terrain or possibly other objects) 0 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