Representing damage on models

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.

This is both simple and complex. You have to create a new material for each box. (complex part)

Simple part is that you simply add this into the health script of the box. On it’s start, it creates its own material and manages it, nothing complex.

I have already 10 different materials
This will be mess

I think you are thinking complex… no really, its simple.

You state, each object has a health. That is a script. (or at least it should be)

Just add in the material controller to that script.

Hi Huder,

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:

boxRenderer.material.mainTexture = damageTextures[imageNumberToUse];

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.

I hope this makes sense…

Chris

This will replace main texture so with 10 different materials i would have to make 30 different damage textures.

Oh ok, I understand the issue now…

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.

You should then be able to use:

boxRenderer.material.SetTexture("_DecalTex", damageTextures[imageNumberToUse]);

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…

Chris

Hi Huder,

I tried it and it works fine, here is what I did…

  1. I added a new cube GameObject
  2. I set a matrial for it and changed the shader to "Decal (left the decal texture empty)
  3. 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]);
		}

	}
}
  1. I set the DamageTextures array in the inspector to 1 long and added a “splat” transparent texture

1624864--99663--$Script.jpg

This is what happened when I pressed the “t” key :

Before :

After:

Hope this helps you… the screenshot of the game “arena” looks fun!

Chris

Thank you very much it works but where come from string “_DecalTex” ?

Edit: Oh no this shader is not working on my android 2.3.1 :confused: but if someone have newer device this will work?

Unfortunately, it comes from the shader and they are not always easy to find. It is a wonderful argument that these are not better documented.

Ok i got another idea :smile:
I will child another box model slightly bigger than parent (with main texture) and that child will have particle material with transparent damage texture.

Hi Huder,

Yep not tried this shader on the mobile… what platform are you using?

Edit : (oh sorry just saw - Android)

chris

That sounds like an idea :slight_smile:

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.

Let us know how it goes

Is working very well, just tested on android. You must be very close to see what is going on.

Excellent Huder, glad it is working… looking forward to seeing your game :slight_smile: