Unity3D Building Damage Tutorial- Part III - Simple Damage

So, lets add a simple damage, really easy thing to just test what we have. For Example, lets make a function that will delete the given percent of height. If you will pass 0.01 parameter, the verticles in upper 1 % of mesh will dissappear.



First, i changed the cube, to more complicated primitive, a capsule, like this on photo.

Obviously, we have to add a damagemaking method, but before lets look at  function in MeshManager Class.

CalculateMinimumHeight method
As you may remember, it was called in the constructor: ...                ManageVerticles(mesh.vertices); ManageTriangles(mesh.triangles); CalcualateMinimumHeight; } Before we will look at code, I have to make something clear. Despite its name, it calculates both minimum AND maximum height. Its obvious when you see it.

public void CalcualateMinimumHeight{ Vector3[] verticles = mesh.vertices; foreach(Verticle vert in Aliases){ if(vert.positionAbsolute.y < MinimumHeight){ MinimumHeight = vert.positionAbsolute.y;} if(vert.positionAbsolute.y > MaximumHeight){ MaximumHeight = vert.positionAbsolute.y;} } 	} Well, its simple. First a foreach loop, than asking every Alias about its absolute height (positionAbsolute.y). If it is higher then our current MaximumHeight, MaximumHeight is set to this height. Similarly it is done with Minimum Height.

Destroy method
public void Destroy (float percent){ float DestroyHeight	= MaximumHeight -(MaximumHeight - MinimumHeight)*percent; foreach(Verticle vert in Aliases){ vert.DestroyByHeight(DestroyHeight); } 		UpdateTrianglesList; }

First, we are calculating the DestroyHeight: when Alias is higher than it, it should be destroyed.

Than, we start a foreach loop and call the DestroyByHeight method to every Alias. Now, it is time, to look at this function, in Verticle.cs

DestroyByHeight method (Verticle.cs)
public void DestroyByHeight(float height){ if(height<positionAbsolute.y){ Destroy; } 		} Method gets one variable. Check if Global Height of Alias is bigger than height. If so, starts Destroy method.

Destroy (Verticle.cs)
private void Destroy{ foreach(int k in LinkedAliases){ OwnerManager.Aliases[k].RemoveLink(number); RemoveLink(k); foreach(int l in Triangles){ OwnerManager.Aliases[k].RemoveTriangle(l); } 			} 			DestroyTriangles; } Oh, this is intresting. So what is done here?. First, we start foreach look taking every int in LinkedAlias (which stores the verticle [verticle, NOT Alias] numbers which this Alias is connected to. Than, we have

OwnerManager.Aliases[k].RemoveLink(number); public void RemoveLink(int k){ int index = LinkedAliases.IndexOf(k); LinkedAliases.Remove(index); } 				foreach(int l in Triangles){
 * Here we check which Alias has this verticle.
 * Than we send an info to this Alias, that verticle of this number should be removed from LinkedAliases list. Lets look at this function.
 * Oki, than we have to remove links from this Alias. In fact, as we teoretically delete this, this is not necessary, but i still insert this removal here. Maybe we will Re-Remove it later?
 * Here we make foreach loop in Triangles list. And sent info about this triangle being destroyed to the current focused LinkedAlias [k]

OwnerManager.Aliases[k].RemoveTriangle(l); }

RemoveTriangle method(Verticle.cs)
public void RemoveTriangle(int number){ if(Triangles.Contains(number)){ Triangles.Remove(number); } 		} We see if our Triangle List contains giver triangle number. If so, deletes it.

Back to Destroy (Verticle.cs)
Now, we stat last method here: DestroyTriangles. See, first we tell all other Aliases that our links and triangles will cease to exist. Now we will tell the MeshManager about it, and he will pass this info to Unity.

DestroyTriangles method (Verticle.cs)
private void DestroyTriangles{ for(int i = 0 ; i<Triangles.Count; i++){ int k = Triangles[i]; OwnerManager.RemoveTriangle(k); }		 		} We just simply send info to OwnerManager, that it should delete the triangle from its data. We pass the number of triangle as k. It is in  for loop, so it will be made to every triangle.

RemoveTriangle method
public void RemoveTriangle(int k){ Vector3 triangleNumbers = Triangles[k]; Vector3 aliasNumbers = ChangeVerticleToAlias(triangleNumbers); Triangles[k] = Vector3.zero; } well lets see.
 * We get a Vector3 with Verticle numbers which make this triangles.
 * We translate it to AliasNumbers.
 * You see, this two above are not necessary, as i am not using it. But maybe i will use it later
 * This is interesting. I set all numbers here to zero. As a result, there will be no triangle. Teoretically, I could remove this Vector3 compleatly, but it will affect overal triangle numbers.

Back to Destroy method
Now we start our last function. 		UpdateTrianglesList;

UpdateTrianglesList;
public void UpdateTrianglesList{ int LenghtOfTrianglesArray = Triangles.Count*3; int[] tempTrianglesArray = new int[LenghtOfTrianglesArray]; int i = 0; foreach(Vector3 vec in Triangles){ tempTrianglesArray[i] 	= (int)vec.x; 			tempTrianglesArray[i+1] = (int)vec.y; 			tempTrianglesArray[i+2] = (int)vec.z; 			i+=3; } 		mesh.triangles = tempTrianglesArray; } This function translates the Triangles list to mesh.triangles. What is intresting, the meshManager and Aliases should operate only on Triangles list.Than, this function should be called that will translate list and assign it to mesh.triangles.

Starting Destroy function
I am calling it in Building.cs, line 182, and i am doing it for each meshfilter. public void FireDamage{ foreach(MeshManager manager in meshManagers){ manager.Destroy(0.02f); } All classes of building like WarFactory or Refinery are deriverd from  Building class. The FireDamage method is called when you click on building. So, lets see the result.

End
Well, still there are some things to do. First, if we rotate the object, the same verticles will dissappear. Second, I belive the Alias still holds the data about its Triangles.