Project Description


Title: Raycast, Bullet Holes, and Random Arrays in Unity 3d
Length: 08:24

In this Xenosmash 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.

Hello everyone welcome to another Xenosmash unity tutorial in today’s unity tutorial we will be showing you how to create a raycast and how to use the random range to randomly assign a texture to be instantiated from an array. So lets take a look at what we’re going to be creating so as you can see here from my gun model I have this raycast being shot out. Now if I left click on this plane you can see that these textures being instantiated. I have a couple of different types of blood textures and I also have a bullet hole texture alright lets go ahead and see how this works. So in my first person controller I have my main camera with my 3d model here. This is a model of a Desert Eagle that I downloaded for free off and right in front of the gun I have an empty game object now it’s aligned fairly well with the barrel and the raycast will actually be applied to this game object so first lets go ahead and create our texture, and well do that by going up to game object > create other > plane and set the position to 0, 0, 0. Press enter and select it and press “F” to zoom in on it so I have my bullet hole texture here and I’m just going to drag and drop it here as you can tell we have a lot of this texture looks pretty square and we just want the bullet hole to be visible so you want to make sure the shader is set to a transparent shader and select diffuse then all we have left is our bullet hole use a png if you want this type of bullet hole texture so we need to create an empty game object now and you want to make sure the position is also set to 0, 0, 0 these are going to be stacked onto each other so I’m going to rename this empty game object bullet hole parent and I’m going to take the plane and make it a child object of that empty game object. Now select your plane again and you want to make sure the position in the “Y” is set to 0.1 now this will raise it slightly above our empty game object and this will just make it a little bit easier for our textures to be instantiated onto other objects in our scene. So once that’s done go ahead and create a prefab of it. Right click > Create > Prefab I’m just going to name it bullet hole 2 since we already have a bullet hole just drag and drop it into there to make a prefab and delete the original so I’m going to scale it down since I forgot to do that before set it to .2 by .2 by .2 alright now lets go ahead and create our script this is our first script we’re going to be creating and our first variable called “bulletTex” and it’s of type “GameObject” and we have an array attached to it and in the Update Function we have a variable called fwd = transform.TransformDirection(Vector3.forward); all this is doing is casting our ray in the z axis or forward in this case off of our empty game object then we have var hi aycastHit; and this line here is not necessary but it’ll help us visually see our raycast. So Debug.DrawRay(transform.position, fwd * 10,; this determines the length of our ray. So right now it’s set to 10 and this last part determines what color our ray is visibly going to look like after we have our if(Input.GetButtonDown (“Fire1”) && Physics.Raycast(transform.position, fwd, hit, 10)) this is saying if we hold the left click button and we’re also hitting a collider with our raycast then we’re going to instantiate our texture so right here it says bulletTex[Random.Range(0,3)] now what this is saying is it will randomly select a game object from our array and instantiate it. So here zero denotes the minimum number and three denotes the maximum number it’s going to select between 0 and three and instantiate that number so if whatever texture is selected as number two if this decides to pick number two then that will be the texture that will be instantiated then we have hit.point, basically where we left click or where our raycast hits on our object, then we’re going to say Quaternion.FromToRotation(Vector3.up, hit.normal) Quaternion.FromToRotation(Vector3.up, hit.normal) it’s going to apply that same rotation to our texture to that of our object so that it’ll match the object that we’re hitting So once that’s done go ahead and save it. For our destroy script we have function awake and we just have destroy(gameobject, 5); five is just the amount of seconds we have before the game object is destroy. Now you want to make sure that this is either in awake or start if you have this in the update function it’ll just keep checking over and over even after its destroyed the game object save both the scripts and minimize I’ll go ahead and remove the script from the RaycastEmpty I’m just going to apply the script again so that we can see what it looks like. So we have “bulletTex” now the size you want to make sure that the size is set to the amount of textures that you want. Since we’re going to be using three press enter. Now the one thing you need to note is whatever size you pick right there needs to equal this as well both of these need to match. If you have the size set to six and this is set to three, this will only randomize three different types of textures versus the six that you could have so make sure both those match up. Lets select our game objects now select blood one, then select the two bullet holes now lets hit play and there we go. There’s our raycast being shot out of our gun. Now if I left click we can see taht different types of textures are being applied as you can tell that one texture is still quite big, but you can see that it’s randomly selecting three different types of textures now if you go into the two by three layout. Go ahead and select gizmos on in your game window and go ahead and select your top view in your scene window. hit play and you can see how far out your raycast goes. As you can see it goes out pretty far in the scene window if we move in a little bit closer you can see that right about there is where our raycast reaches this wall so you can see I’m creating these textures on that wall from this far out that is the basics of using a raycast in Unity3d and how to instantiate random textures using the random.range in your script So I hope you enjoyed this tutorial, thanks for watching!