- Home /
Terrain Collider doesn't update/ how to create a new terrain Collider?
Hi,
I'm trying to create an modify terrains at runtime. So far I've donne this:
//Time to create the first terrain based on the template that is in the scene
tempTerr = Instantiate(TerrainTemplate, new Vector3(2000.0f, 0.0f, 2000.0f), new Quaternion(0.0f, 0.0f, 0.0f, 0.0f)) as Terrain;
tempTerr.terrainData = Instantiate(TerrainTemplate.terrainData) as TerrainData;
hMp = generateHeightmap(tempTerr.terrainData.GetHeights(0, 0, 513, 513), new Vector2(513.0f, 513.0f));
tempTerr.terrainData.SetHeights(0, 0, hMp);
At the moment the terrain is being created, and I can see the changes to the terrain (created by the generateHeightmap function). The Problem is that the player falls throught the terrain and stops at the bottom of the terrain(where the old terrain collider was(A flat terrainCollider)).
Bottom line: how do I update the terrain collider? The SetHeights function should do that but it doesn't. What can I do? A little/fast code example would be greatly appreciated.
Thanks for your time...
edit: I've already tried Flush() but nothing changed.
Answer by roamcel · Aug 05, 2011 at 12:21 PM
I'm sorry if I can't be more helpful, but I believe you need to manually update the terraincollider, based on the new heightmap (never faced this problem so mine is basically a guess).
Thanks for trying to help but the collider in the terrain is read only, ID$$anonymous$$ what else I can do, guys please help me xD
I'm trying to find this out also. Did you ever find a solution to this?
Answer by Darkgaze · Mar 26, 2019 at 11:18 AM
For anybody looking for help, in v 2017, 2018.. to update the TerrainCollider you have to update the heights on the terraindata of both the terrainCollider and the terrain. Seems like there is a copy. Changing the terrain only will update the visualization but not the collider itself.
terrainCollider.terrainData.setHeights(...)
terrain.terrainData.setHeights(...)
In v2019 the terrain has been moved to GPU and the API changed, so maybe this doesn't work. Seems like there is a function to update "dirty" data.
Your answer
Follow this Question
Related Questions
Unity Terrain Update 0 Answers
How would i go about meshing over several objects? 1 Answer
I have on my terrain toolbar only four items instead of seven how can I change that 0 Answers
Player falling through Terrain collider 0 Answers
How to raise/lower a part of the terrain through script 0 Answers