- Home /
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.
Does anyone know what is going on?
There still has not been anything posted about this issue and how to solve it.
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();
}
}
Your answer
Follow this Question
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