I have a simple box objects and balls, these boxes have hp variable. If ball hits box then this will cause damage to it.
I don’t know any simple method to show this damage. I thought about to have 3 textures with transparent like that:
And multiply blend this with box texture. But this can’t affect rest of box models, only the one that ball hits.
Also i have to mention that boxes are dynamic rigid bodies. I’m working on android mobile game.
The easiest would be to have a public array of textures as a parameter and to set them to your textures in the inspector, you will also need a reference to your box’s mesh renderer (you would just assign your box object to this parameter and it will find the mesh renderer component) :
public Tecture2D[] damageTextures;
public Renderer boxRenderer;
Then in your script, when the health changes and you have decided which texture to use (based on remaining health), you can set it using:
This script will only affect the box that is referred to by the renderer so it wont affect the other boxes in the scene… usually you would be doing this in a script attached to the box gameobject and once you have set the textures and the renderer in the inspector you can save it as a prefab.
You could use the “Decal” shader for the box texture… the decal shader will use a base texture and allow you to overlay a transparent decal texture over the top of it.
As long as the damage texture is tranparent, it should leave the box’s main texture and just set the overlayed decal texture. If you needed to, you could also set the main box texture using the boxRenderer.material.mainTexture property as in the first example.
I have not tried this code myself but will give it a go when I get a chance…
I set a matrial for it and changed the shader to "Decal (left the decal texture empty)
I attached the following script to the object (for simplicity, the script just adds the damage decal when the “t” key is pressed)
using UnityEngine;
using System.Collections;
public class AddDamageDecal : MonoBehaviour {
public Texture2D[] DamageTextures;
void Update () {
if(Input.GetKeyUp("t")){
renderer.material.SetTexture("_DecalTex", DamageTextures[0]);
}
}
}
I set the DamageTextures array in the inspector to 1 long and added a “splat” transparent texture
This is what happened when I pressed the “t” key :
Ok i got another idea
I will child another box model slightly bigger than parent (with main texture) and that child will have particle material with transparent damage texture.
Just remember if the outer box is too close to the inner one you might get some glitches appearing (specially at greater distances) you can often adjust the near and far clipping plane of the camera to lessen the glitches/artifacts if they occur.