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:
using UnityEngine;
using System.Collections;
public class GunShooter : MonoBehaviour
{
public GameObject decalHitwall;
public int bulletDamage = 100;
public float floatInFrontOfWall = 0.001f;
void Update ()
{
RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 0.0f));
if (Input.GetMouseButton(0))
{
if (Physics.Raycast(ray, out hit, 10000))
{
GameObject particleClone = (GameObject)Instantiate(decalHitwall, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 10);
hit.transform.SendMessage("ApplyDamageToEnemy", bulletDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
There are many ways in which you can achieve the same effect .I am gonna tell a few very simple technique
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
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.
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?
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.