Raycast, Bullet Holes, and Random Arrays

Sharing is caring:

Randomize Bullet Holes with Arrays

In this Unity┬átutorial we will be going over a few different things. First we’ll discuss how to create a Raycast that we’ll apply to the front of our weapon. Then we’ll create bullet hole textures that will be randomly instantiated from an array using random.range in our script. This is useful for creating a random type of bullet texture every time we shoot.

This makes it look more realistic as not every bullet hole will look exactly the same every time. Start off by applying an empty game object to the front of your gun. We will create our raycast off of this empty game object. In this script we’ll have the raycast visible in order to see how far it reaches and also to show you how a raycast works.

We adjust the length of this raycast to 10 meters and a color of green. Afterward we’ll include an array for our bullet hole textures. The reason we use an array is so that we can have more than one bullet hole texture to be instantiated. Whenever we shoot it will randomly instantiate a bullet hole texture, which looks more realistic. The bullet hole textures will rotate to fit the rotation of the object we “shoot” at. Below are the scripts used in this video:



Original Bullet Hole texture (we used an edited one in the tutorial from this image)

Bullet Hole Image from CG Textures


Javascript Files


var bulletTex : GameObject[]; // creates an array to use random textures of bullet holes

function Update () {

   var fwd = transform.TransformDirection(Vector3.forward); //casts our raycast in the forward direction
   var hit : RaycastHit;
   Debug.DrawRay(transform.position, fwd * 10, Color.green); //drays our raycast and gives it a green color and a length of 10 meters

if(Input.GetButtonDown ("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10))
{ //when we left click and our raycast hits something
         Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); //then we'll instantiate a random bullet hole texture from our array and apply it where we click and adjust// the position and rotation of textures to match the object being hit

Jonathan Gonzalez

I love all things in game development and want to contribute by teaching others how to create games. I'm always looking to help others create awesome digital worlds. I've been a hardcore gamer since I was a kid and now it's my turn to create awesome digital experiences.

More Posts - Website

Follow Me:


  1. BULLETHOLES.JS is a one liner. It’s unreadable. Can you perhaps fix the sample code? Thanks!

  2. Some reason the size doesn’t show when I place the script on RaycastEmpty??
    Also do you need to assign the destroy script to anything.

    • Size of what? If you’re referring to the length of the raycast that is all done within the script. You won’t see a variable for it in the inspector panel, it’s purely used for providing a visual tool to test out our raycast. Also make sure that your empty game object is pointing in the right direction. The Z direction needs to be pointing forward, this is where the raycast will be pointing. The destroy script should be applied to your bullet hole texture prefab. It’s not required, but it prevents having too many texture objects in your game which in turn means better performance.

  3. Armand Duimessand says:

    Hi, how do I reformat the code to C#? Sorry, I’m new to programming

    • C# in Unity is very similar to UnityScript. So in this case you’d have to reverse a few things. Instead of “var bulletText : GameObject[];” you’d use “public GameObject[] bullexText;”. “var fwd” is actually a Vector 3 so you’d write “Vector3 fwd” then write out the rest in the same way. Lastly functions would be “void Update” instead of “function Update”. If you’re looking to learn C# from a beginner perspective you can watch this course I created on using C# in Unity: https://cgcookie.com/course/c-bootcamp-for-unity/

Speak Your Mind