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

[C#] How to add Bullet Decals after gun is shot? (HELP!)

Discussion in 'Scripting' started by KyleStank, Jun 5, 2014.

  1. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    I have been trying to make this work for a while now. I am very close but the only thing that is not working is when I shoot, the bullet decal goes into the wall too much and sort of blends in with another texture so it looks funny when the player walks around. How do I instantiate the bullet decal on the Raycast's shot position with the decal barely sticking out so no visual glitches occur? Here is the code I am using:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GunShooter : MonoBehaviour
    5. {
    6.     public GameObject decalHitwall;
    7.     public int bulletDamage = 100;
    8.     public float floatInFrontOfWall = 0.001f;
    9.  
    10.     void Update ()
    11.     {
    12.         RaycastHit hit = new RaycastHit();
    13.         Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 0.0f));
    14.  
    15.         if (Input.GetMouseButton(0))
    16.         {
    17.             if (Physics.Raycast(ray, out hit, 10000))
    18.             {
    19.                 GameObject particleClone = (GameObject)Instantiate(decalHitwall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
    20.                 Destroy(particleClone.gameObject, 10);
    21.                 hit.transform.SendMessage("ApplyDamageToEnemy", bulletDamage, SendMessageOptions.DontRequireReceiver);
    22.             }
    23.         }
    24.     }
    25. }
    26.  
    Please help!
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Option 1: Use a decal material with a shader using z-offset property.
    Option 2: offset the decal a short distance along the RaycastHit normal.

    What you describe is called z-fighting and happens when geometry occupy the same space.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    he's doing that

    Code (csharp):
    1.  
    2. publicfloat floatInFrontOfWall = 0.001f;
    3. ...
    4.  
    5. ... hit.point+(hit.normal* floatInFrontOfWall) ...
    6.  
    perhaps by not enough? does it still happen if you make floatInFrontOfWall something a little bigger?
     
  4. deltamish

    deltamish

    Joined:
    Nov 1, 2012
    Posts:
    58
    Hi,

    There are many ways in which you can achieve the same effect .I am gonna tell a few very simple technique

    1. You can directly place a texture over another texture but you will nedd pro for that
    2 .Another easy way is to Create a new Camera make it child of main camera and set its culling mode to Depth only.
    Then create a new layer an name it as Decal and set the camera culling layer to Decal and uncheck everything else

    Another a bit complicated way is to offset it according to viewing angle
     
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Apologies, I missed this.
    Still, using a shader that takes care of this automatically is the way to go.
    http://docs.unity3d.com/Manual/SL-CullAndDepth.html Read under Offset there.
    You could also use a shader with a vertex push based on camera distance to do this.
     
  6. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Instantiating bullet decals is not a good idea at all. Use a shader as ThermalFusion suggested
     
  7. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    Thank you all for your replies. But I am still a bit confused on how to use a shader. I am still pretty new to Unity as I have only made 1 game only for fun. I never used a shader for anything before because I have never had to. But now I need to for this. Is there any example code or YouTube tutorials on how to do something similar or something like this on shaders?
     
  8. Jribs

    Jribs

    Joined:
    Jun 10, 2014
    Posts:
    154
    giving this a bump because I am looking for help with basically the same thing.
     
  9. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Download the built-in shaders.
    Copy and rename one of the transparent ones.
    Edit the shader:
    Change the shader name, so you don't replace any built-in.
    Add the offset, usually under the tag declarations
    Offset -1, -1
    Make a material using this shader.
    Assign material to quads spawned at hit.