Hi, I’m making my own terrain data and I want to apply collision data to it.
I have it sort of working, but I need some more info. Can anyone help?
I realised that the height data needs to be in the 0-1 range and the scale is the ‘size’ Vector3.
But I don’t know the orientation of the terrain or the order the terrain vertices are in, or whether the position is the centre of the terrain or a corner?
I don’t know if needs rotating, but I have tried every angle there is, and it’s seems to be off by quite a bit.
My patches are 65x65 is size.
What I really want is a way of Rendering the Terrain Collision triangles.
Thanks,
Dave H.
For each patch I do…
coll = GetComponent<TerrainCollider>();
coll.terrainData = new TerrainData();
coll.transform.position = transform.position; // Is this the centre or an edge?
coll.terrainData.heightmapResolution = widthGrid; // 65x65
coll.terrainData.size = new Vector3(patchWidth, 80.0f, patchWidth);
heights = coll.terrainData.GetHeights(0, 0, (int)widthGrid, (int)widthGrid);
// Later...
int i = 0;
for (int z = 0; z < widthGrid; z++)
{
for (int x = 0; x < widthGrid; x++)
{
float h = vertices[i].y / 80.0f;
h = Mathf.Clamp(h, 0.0f, 1.0f);
heights[x, z] = h;
i++;
}
}
coll.transform.position = transform.position;
coll.terrainData.SetHeights(0, 0, heights);
coll.terrainData.physicMaterial = null;