Raycasting
--
We are going to look at raycasting. Raycasting happens when you fire a projectile in a 3D line and it tells you what was hit. Shooting, AI driving, and many more happen via raycasting.
There are up to 16 different overrides when using Raycasting.
We are going to create a scene to demonstrate raycasting. The first thing we are going to do is add a cube to our scene.
A raycast has two key components a ray origin and a RayCastHit that stores information about what we hit.
We are going to create a player C# script that says if the left mouse button is clicked tell us where the current position of the mouse is on the screen and if it detects a collider on an object tell us what that object is and change it’s color.
We are now going to add multiple objects to our scene of different shapes. A sphere, a capsule, and another cube. We want to only change cubes random colors, the sphere nothing will change, and the capsule will turn black.
A great way to tell game objects apart is to assign them a tag. So we will give the sphere the sphere tag…etc. In the editor we will create and add a tag to its respective object. Back in our Player script we will remove the code from within the raycast and add a hitRenderer which will tell that we hit something and a switch statement for our shapes with the desired function based upon it’s tag.
Click to Instantiate
We are going to create a sphere at the position we clicked on a floor.
To do this we are going to need to create a cube and scale it out to be out floor. Next we are going to create a sphere prefab and a material for the sphere. Let’s color it red. Next we are going to create a PlayerInstance script and attach it to our main camera.
Here we are going to create a variable that let’s us attach our prefab. In our update function we are going to say if we left clicked our mouse, we are going to take the current position of our mouse and create a sphere at that location. We use our location data and the collider of the floor to say we want to place the sphere on the floor.
Layer Masks
Layer masks allows us to only attack what we want it to.
We are going to create 3 cubes. 2 of them will be the labeled as enemies and 1 will be the player.
For this event when we click on an enemy we are going to turn it red. In the inspector for the enemy we are going to create an enemy layer in layer 6. Do this for Layer 7 as well for the player.
Next we are going to create a PlayerMask script and attach it to the camera.
In this script we are going to do a lot of the same as before. We are going to get our current mouse position, our ray origin relative to where it is on the camera, and our hit info. The new piece here will be that when we are doing are raycast we have to detect which layer we are casting it to. If it is the 6th layer we turn the enemy red. If it is the 7th layer it will be blue.
Directional Rays
The first thing we are going to do is build a floor. We will add a sphere as well.
We will then create a sphere script to go along with it.
Now we will do our raycast with the direction facing down. Saying that when we drop a distance of 1 we are to hit something. Using OnDrawGizmos creates a blue line of the distance one in our scene view for us.
Now on our sphere we are going to want to add a rigidbody to it as well as turn off gravity and turn on isKinematic
First we need to be able to call our rigidbody from the script. Next we will access the rigidbody itself and set our desired changes in the script to gravity and isKinematic.
Note: When using Physics using FixedUpdate is appropriate.
We had to change our debug to draw ray, because when we were dropping our sphere it was falling faster than our gizmo would and the sphere would not stop at the right height. Now we are drawing a ray that will stop at 3 units above our floor.
Bullet Holes
Now we are going to create bullet holes for our level. Now not everyone will have access to the items I will be using, but you can find suitable tools online. We are first going to create 2 cubes and create a wall and floor and put them together. Then we are going to import from FileBase 3 things. One the FPS Controller, BulletHoles, and Reticles.
For the reticles we will create an UI image and select the reticle of our choosing for our sight to point at the wall. We will then remove our main camera and import our FPS Controller that has a main camera attached to it. Next we will create a Shoot script and attach to our controller. We will create a location to place our bullet holes in this script.
From here when we click the left mouse button we will fire from the center of the camera a shot and a bullethole will render.
The bulletholes are glitching in the wall, but we will not be covering that at this time.
Moving using click
We will now be moving our player anywhere on the floor that we click too.
For our final part of this tutorial we are going to create two scripts. One will be the PointerScript which will get attached to the main camera and the other script is the PlayerPointer which will be attached to the player.
For our pointer scripts we will first make sure we can identify our player. Then we will cast our ray and make sure we are hitting the floor. Once hit we tell our player where on the floor to move to which is our updated destination.
For our player we have to have a target destination for him. That location is a vector 3. We find the distance we are moving by getting our new location and subtracting our current position from it.
There you have it folks! Here is your introduction to raycasting!