Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Representing damage on models

Discussion in 'Scripting' started by Huder, May 12, 2014.

  1. Huder

    Huder

    Joined:
    Jan 8, 2013
    Posts:
    12
    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.
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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.
     
  3. Huder

    Huder

    Joined:
    Jan 8, 2013
    Posts:
    12
    I have already 10 different materials
    This will be mess
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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.
     
  5. CplMulder

    CplMulder

    Joined:
    May 12, 2014
    Posts:
    52
    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) :
    Code (csharp):
    1.  
    2. public Tecture2D[] damageTextures;
    3. public Renderer boxRenderer;
    4.  
    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:
    Code (csharp):
    1.  
    2. boxRenderer.material.mainTexture = damageTextures[imageNumberToUse];
    3.  
    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
     
  6. Huder

    Huder

    Joined:
    Jan 8, 2013
    Posts:
    12
    This will replace main texture so with 10 different materials i would have to make 30 different damage textures.
     
  7. CplMulder

    CplMulder

    Joined:
    May 12, 2014
    Posts:
    52
    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:
    Code (csharp):
    1.  
    2. boxRenderer.material.SetTexture("_DecalTex", damageTextures[imageNumberToUse]);
    3.  
    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
     
  8. CplMulder

    CplMulder

    Joined:
    May 12, 2014
    Posts:
    52
    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)
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class AddDamageDecal : MonoBehaviour {
    6.  
    7.     public Texture2D[] DamageTextures;
    8.      
    9.     void Update () {
    10.    
    11.         if(Input.GetKeyUp("t")){
    12.             renderer.material.SetTexture("_DecalTex", DamageTextures[0]);
    13.         }
    14.  
    15.     }
    16. }
    17.  
    4. I set the DamageTextures array in the inspector to 1 long and added a "splat" transparent texture

    $Script.jpg

    This is what happened when I pressed the "t" key :

    Before :
    $BoxBefore.jpg

    After:
    $BoxAfter.jpg

    Hope this helps you.... the screenshot of the game "arena" looks fun!

    Chris
     
  9. Huder

    Huder

    Joined:
    Jan 8, 2013
    Posts:
    12
    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 :/ but if someone have newer device this will work?
     
    Last edited: May 12, 2014
  10. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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.
     
  11. Huder

    Huder

    Joined:
    Jan 8, 2013
    Posts:
    12
    Ok i got another idea :D
    I will child another box model slightly bigger than parent (with main texture) and that child will have particle material with transparent damage texture.
     
  12. CplMulder

    CplMulder

    Joined:
    May 12, 2014
    Posts:
    52
    Hi Huder,

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

    Edit : (oh sorry just saw - Android)

    chris
     
  13. CplMulder

    CplMulder

    Joined:
    May 12, 2014
    Posts:
    52
    That sounds like an idea :)

    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
     
  14. Huder

    Huder

    Joined:
    Jan 8, 2013
    Posts:
    12
    Is working very well, just tested on android. You must be very close to see what is going on.
     
  15. CplMulder

    CplMulder

    Joined:
    May 12, 2014
    Posts:
    52
    Excellent Huder, glad it is working... looking forward to seeing your game :)