Created
January 18, 2018 04:47
-
-
Save DCCoder90/7a5446f73438ef91d6ecd967574f7f7c to your computer and use it in GitHub Desktop.
A collection of helpers for Terrain math with Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class TerrainMath{ | |
/** | |
* Gets the XBase and width based on bounds input | |
* @param portalBounds The bounds of the Box collider to use | |
* @param terrainBounds The bounds of the terrain collider to use | |
* @return Vector2 Where X is the XBase and Y is the Width | |
* @static | |
*/ | |
public static Vector2 GetXBaseAndWidth(Bounds portalBounds, Bounds terrainBounds){ | |
float xbase,width; | |
if(portalBounds.min.x<terrainBounds.min.x){ | |
xbase=terrainBounds.min.x; | |
width=portalBounds.size.x-(terrainBounds.min.x-portalBounds.min.x); | |
}else if(portalBounds.max.x>terrainBounds.max.x){ | |
xbase=portalBounds.min.x; | |
width=portalBounds.size.x-(portalBounds.max.x-terrainBounds.max.x); | |
}else{ | |
xbase=portalBounds.min.x; | |
width=portalBounds.size.x; | |
} | |
return new Vector2(xbase,width); | |
} | |
/** | |
* Gets the ZBase and height based on bounds input | |
* @param portalBounds The bounds of the Box collider to use | |
* @param terrainBounds The bounds of the terrain collider to use | |
* @return Vector2 Where X is the ZBase and Y is the Height | |
* @static | |
*/ | |
public static Vector2 GetZBaseAndHeight(Bounds portalBounds, Bounds terrainBounds){ | |
float zbase,height; | |
if(portalBounds.min.z<terrainBounds.min.z){ | |
zbase=terrainBounds.min.z; | |
height=portalBounds.size.z-(terrainBounds.min.z-portalBounds.min.z); | |
}else if(portalBounds.max.z>terrainBounds.max.z){ | |
zbase=portalBounds.min.z; | |
height=portalBounds.size.z-(portalBounds.max.z-terrainBounds.max.z); | |
}else{ | |
zbase=portalBounds.min.z; | |
height=portalBounds.size.z; | |
} | |
return new Vector2(zbase,height); | |
} | |
/** | |
* Converts and X coordinate in world space to a terrain coordinate X | |
* @param XPositionToConvert The X position in world space to convert | |
* @param TerrainToUse The terrain to convert the X position to | |
* @return int The converted position | |
* @static | |
*/ | |
public static int ConvertXPositionToTerrainCoordinates(float XPositionToConvert, Terrain TerrainToUse){ | |
float terraincoordinate = (XPositionToConvert-TerrainToUse.transform.position.x)/TerrainToUse.terrainData.size.x; | |
return (int)(terraincoordinate*TerrainToUse.terrainData.heightmapWidth); | |
} | |
/** | |
* Converts and Z coordinate in world space to a terrain coordinate Z | |
* @param ZPositionToConvert The Z position in world space to convert | |
* @param TerrainToUse The terrain to convert the Z position to | |
* @return int The converted position | |
* @static | |
*/ | |
public static int ConvertZPositionToTerrainCoordinates(float ZPositionToConvert, Terrain TerrainToUse){ | |
float terraincoordinate = (ZPositionToConvert-TerrainToUse.transform.position.z)/TerrainToUse.terrainData.size.z; | |
return (int)(terraincoordinate*TerrainToUse.terrainData.heightmapHeight); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment