Problem with drawing bullet marks (SOLVED)

Hi I´m very new to Unity and need some help with a script.
I want to draw bullet marks using raycasting. In the Unity Script Reference I found this Script :

// Attach this script to a camera and it will paint black pixels in 3D
// on whatever the user clicks. Make sure the mesh you want to paint
// on has a mesh collider attached.
function Update () {
// Only when we press the mouse
if (!Input.GetMouseButton (0))
return;

// Only if we hit something, do we continue
var hit : RaycastHit;
if (!Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit))
return;

// Just in case, also make sure the collider also has a renderer
// material and texture. Also we should ignore primitive colliders.
var renderer : Renderer = hit.collider.renderer;
var meshCollider = hit.collider as MeshCollider;
if (renderer == null || renderer.sharedMaterial == null ||
renderer.sharedMaterial.mainTexture == null || meshCollider == null)
return;

// Now draw a pixel where we hit the object
var tex : Texture2D = renderer.material.mainTexture;
var pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;

tex.SetPixel(pixelUV.x, pixelUV.y, Color.black);

tex.Apply();
}

OK, but now I don´t want to set a pixel to black. I would like to put a little texture on the impact point. This texture should be put on top of the main texture. I saw that in a game made with Unity. I really tried to get this working, but I was not successful and so have the hope that you can help me. Sorry for my english, I hope you understand me. Thanks for the replys.

Rewriting a whole bunch of pixels isn’t a good way of going about this; It’s slow (and you can’t even use it with compressed textures) and doesn’t work with tiling. What is typically done is instantiating a quad that represents the full texture, and alpha blending it onto wherever it goes. Raycasting is still good for this, so you can start with the code you already have. I’m sure there’s something about it here or on the wiki, because it’s so common, but I’ve never looked for it.

In the meantime, you should vote this up, so it can become easier/more robust in the future.

Hi Jessy

thank you for your reply.

Your link is very interesting, and you are right changing pixels is not the right way.
I will look in the wiki. I think instantiating a quad is a good idea.

Does anybody know how to orient a plane to the normal of an object ? That would be very usefull.

The RaycastHit object has a field for the normal of the surface at the hit point. You can orient a plane to this using Quaternion.FromToRotation:-

plane.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);

There is also a script in this thread that implements an efficient four-vertex plane object that you may find useful for the bulletholes.

Thanks

I solved the problem using http://forum.unity3d.com/viewtopic.php?t=54613&highlight=decal+system
It works great !!