How can I darken a GameObject (Building) as it gets damaged?. For example every time it gets hit the texture gets darker and darker to display how damaged the building is.
//script must be attached to your building
public class BuildingDamager : MonoBehavior{
Material buildingMaterial;
void Awake(){
buildingMaterial = this.getComponent().material; //notice, not shared material
}
//invoke this function from anywhere outside or even from inside this particular script instance
public void Darken(float percent){
percent = Mathf.Clamp01(percent);
buildingMaterial.color = new Color(buildingMaterial.color.r* (1 - percent), buildingMaterial.color.g* (1 - percent), buildingMaterial.color.b* (1 - percent), buildingMaterial.color.a) ;
}
}
You want to have a look at Procedural Materials, they’re made with Substance Tools from Allegorithmic and very cool for this kind of effect.