Unity Guide Part 3
Raycasting

by Connor Killingbeck

 

Alright, listen up. This is when things get serious. From here on out we are going to be going through some of the more advanced coding you can do in Unity. In this lesson, we will be using Raycasting and the editing of another objects Components. So gear up and get ready for another happy landing.

 

First things first is to make a new 3D unity project. Once the compiling is done we will start right away by creating a new blank object, I called it “Shooter”. Create a sphere as a child of Shooter and have it be at 0,0,0 in shooters local space. Then create a cylinder as a child of shooter, give the cylinder a scale of 0.5,0.5,0.5, rotate it’s z by 90, and then set its coordinates in local space to -0.8,0,0. It should look something like this:

 

 

Finally, we are going to add a blank gameobject name ShootFrom as a child of the object. Make this objects position in local space be -1.5.

 

 

Boom. We have made a cannon. Yay.

 

Now we only have to make 4 more things than we can start coding I promise.

Finally, create a cube. Give this cube the tag “Toggle”, by clicking on tag, and then creating a new one bu using “Add Tag”. After creating the tag, go back to the cube, click tag, and then select “Toggle”.

 

 

Then duplicate it with control + D 3 times, and then just place them around your gun. Yeah, that's it.

 

 

Boom, I made my cubes skinnier put thats a trivial thing.

 

Now, Its CODING TIME.

 

Create a new C# script called RotateShooter. In this script we will actually use additional functions.

 

First things first, let's make this thing rotate, here is the very simple code that you have done before:

 

 

As you can see. Wait, that's not right. In this case, we are actually going to create a function to make rotating a bit easier, and then we will call that function inside of our update loop. To make a void function in Unity, simply put void FunctionName(passed variables), and that's it. Here is our rotate function:

 

So, in this case, all we do is pass a positive or negative multiplier and it rotates accordingly. Apply this script to our main parent Shooter object and try it out! Everything should rotate, since Shooter is the parent to other objects, all the child object will stay in reference to their parent, rotating as well

 

This is all the help I am legally required to give you on functions. They function more or less the same as they do in java.

 

Now, the final thing we are gonna do is make it so we shoot a Ray via raycast. With that raycast we can manipulate the values of the object the ray hits. This can be changing its position, disabling its renderer, or even changing variables of a script attached to the object! For now we will just have the ray toggle the renderer.

 

Create a new void called Shoot, we will call this whenever we want to.....shoot.

 

 

We will then create an input statement inside of our Update() loop. We want it so that when we press Space, we call the shoot function, like so:

 

 

Now, its shooting time.

 

First, we need to initialize a RaycastHit called hit;

 

 

A RaycastHit is exactly what it sounds. It stores the information from a collision when the raycast HITS a valid object. The RaycastHit will store the point the collision happened, the gameobject it hit, and more, but the latter 2 are the most useful.

 

Now, things get a little bit complicated, we are going to add this if statement. It will look a bit crazy, but I will break it down:

 

 

The above is the first part of our if statement, we will cover the second part after the && in a moment. This is the standard Raycast call, Physics.Raycast is used for 3D raycasting, the first part of the Raycast call is where you want to raycast from. In our case, we have a “From” object inside our Shooter object, so we are going to get its position. transform is the transform of the object that the script is attached to. In this case, we want to get a child of the attached object, so we call .GetChild(2). The reason we use 2 is because the first child is technically child #0. Now that we have the child object referenced, we simply get its transform and then its position. And now, after all that, we will be shooting the raycast from the third child of our objects position.

 

The second variable is the direction we want to shoot the ray, and I know what your thinking, “WHY IS IT NEGATIVE RIGHT?”. This is because of the way we made our canon, our parents forward does not fall along the same line as the cannon barrel, and that is my fault. You will encounter issues like this ALL THE TIME. I decided to leave it in as a thinking point. In our case, left (or negative right) will go along the cannon barrel, so we are shooting that way. Learning to understand angles and how your models are oriented is important, and experimenting is required. Don’t worry however, this will not change our codes efficiency in the slightest.

 

The third statement is out hit. This simply means that the ray that is going out is our hit RaycastHit.

 

Finally, the fourth passed variables is the distance. Most weapons have ranges, but in our case we will pretend that this is a laser cannon. Changing this variable can change how far the ray goes before it stops. This can be very useful for games such as First Person Shooters.

 

Now, to the second part of our if statement:

 

 

This one is very simple. Since we just Raycasted, hit now has all the variables we need stored inside it. In this check, we are checking if the gameobject we have hit, if any object at all, has the tag “Toggle”.

 

Here is the full statement:

 

 

Press Ctrl and + at the same time to zoom in a bit if you are having trouble reading the tiny text.

 

 

Now, we will add the few final lines of code inside of our check.

 

 

This code is simple, we are checking if the gameobject stored in hit has its renderer enabled, and if it does, we are going to turn it off. If the renderer happens to be off, we will turn it on.

 

GetComponent<ComponentName>().VariableInsideComponent is how you access components and scripts. If we had a Script named Color with a public string variable named colorName, we would access that variables by going;

 

GetComponent<Color>().colorName;

 

And so on. Now, if we save our scripts and run, we should see that the cubes are disappearing! And that’s the basics of using raycasting, ray casting is extremely useful and powerful. Raycasting can be used on seperate layers determining distance through objects, or to check to see if your character can see an apple. The possibilities are endless.