unity
runtime_error
ai_generated
true
ArgumentException: TerrainCollider does not support negative terrain height values
ID: unity/terrain-collider-mismatch
88%Fix Rate
85%Confidence
1Evidence
2023-03-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2022.3.0f1 | active | — | — | — |
| 2023.1.0b10 | active | — | — | — |
| 2021.3.20f1 | active | — | — | — |
Root Cause
TerrainData heightmap contains negative values, which TerrainCollider cannot process.
generic中文
地形数据高度图包含负值,地形碰撞器无法处理。
Official Documentation
https://docs.unity3d.com/Manual/class-TerrainCollider.htmlWorkarounds
-
85% success In Editor, select Terrain, open Terrain Settings, click 'Flatten' button to set all heights to 0, then re-paint heights with positive values. Alternatively, use script: TerrainData.SetHeights(0, 0, Mathf.Max(0, heights)).
In Editor, select Terrain, open Terrain Settings, click 'Flatten' button to set all heights to 0, then re-paint heights with positive values. Alternatively, use script: TerrainData.SetHeights(0, 0, Mathf.Max(0, heights)).
-
90% success Use a script to clamp negative heights: var heights = terrainData.GetHeights(0, 0, terrainData.heightmapResolution, terrainData.heightmapResolution); for (int y = 0; y < heights.GetLength(0); y++) for (int x = 0; x < heights.GetLength(1); x++) heights[y,x] = Mathf.Max(0.01f, heights[y,x]); terrainData.SetHeights(0, 0, heights);
Use a script to clamp negative heights: var heights = terrainData.GetHeights(0, 0, terrainData.heightmapResolution, terrainData.heightmapResolution); for (int y = 0; y < heights.GetLength(0); y++) for (int x = 0; x < heights.GetLength(1); x++) heights[y,x] = Mathf.Max(0.01f, heights[y,x]); terrainData.SetHeights(0, 0, heights);
中文步骤
In Editor, select Terrain, open Terrain Settings, click 'Flatten' button to set all heights to 0, then re-paint heights with positive values. Alternatively, use script: TerrainData.SetHeights(0, 0, Mathf.Max(0, heights)).
Use a script to clamp negative heights: var heights = terrainData.GetHeights(0, 0, terrainData.heightmapResolution, terrainData.heightmapResolution); for (int y = 0; y < heights.GetLength(0); y++) for (int x = 0; x < heights.GetLength(1); x++) heights[y,x] = Mathf.Max(0.01f, heights[y,x]); terrainData.SetHeights(0, 0, heights);
Dead Ends
Common approaches that don't work:
-
Resetting TerrainCollider component in Inspector
70% fail
The issue is in TerrainData, not the collider component; resetting doesn't modify heightmap.
-
Deleting and recreating the terrain
90% fail
Loses all terrain data and work; the root cause (negative heights) must be fixed in data.
-
Setting TerrainCollider.enabled = false then true
80% fail
Only toggles collider state, does not address invalid height values.