Hi, I'm fairly new to Unity3d and was wondering how to go about implementing a boolean operation on a mesh equal to the radius of an explosion, or any equivalent effect to achieve a hole in the ground. This is an important element within artillery games. I have a feeling it will be non-trivial, but any help or a point in the right direction would be greatly appreciated. I am using an imported mesh, but I can switch to terrain if it would be easier to implement.
The relevant questions I found suggested moving each vertex within the explosion radius outward, but I'm lost on how to do this. I found http://unity3d.com/support/documentation/ScriptReference/Mesh.html , which is a good starting point, but I need a push in the right direction from here!
Because the terrain system uses a heightmap instead of regular mesh data, you can't modify the vertex positions directly.
Instead you have to adjust the values stored in the heightmap using GetHeights and SetHeights.
For a crater, you'd want to reduce the heights of the area nearest the explosion, but raise the heights around the crater's lip. You'd probably also want to modify the texture used at that position on the terrain.
These separate techniques have been described in other answers on this site, so follow these links for more information:
answered Sep 28 '10 at 08:27 PM
These features of Unity have very little documentation. It took me allot of digging around and experimenting to make a practical deformable terrain. The concept is that Unity terrain stores Height data in a 2 dimensional array [x,y] the height is a float from 0.0 to 1.0 and it can be changed at runtime. To give craters a round concave look we use a texture with an alpha map. We then convert the pixel data from the alpha map into a float ranging from 0.0 to 1.0 depending on how dark the pixel is. Then all that is left is to subtract the crater Height Data from the Terrain Height Data at the impact Vector3 position.
For the textures the process is very similar but it differs in the following way: For every texture you have on your terrain (In this case 2 textures: Grass and Dirt) you must adjust it's alpha value, for example: To get dirt we must make: Grass alpha = 0.0 Dirt alpha = 1.0 to blend grass and dirt we must make: Grass alpha = 0.5 Dirt alpha = 0.5
You can find my implementation here: http://www.youtube.com/watch?v=2W3Vm1_QGf0