Working With Raycast:
Learning how to use Raycast within Unity
In Unity, especially within Unity Games and AR/VR, raycast is an important function which allows users to point and select objects within the virtual scene.
What are Raycast?
Raycast is basically an laser that is sent from usually the player location either from the dead center of the screen or where the mouse cursor location is currently located. The laser would hit an object and return back to the player with data informing what the object the cast hits and where it is on the active scene.
How to script with Raycasting:
In order to understand how Ray casting works, we will do a quick demonstration, we are going to create a 3D cube on the new empty scene and whenever the left mouse button is clicked on the cube, the color of the cube will change.
We are first going to start with creating a basic 3D cube in the scene.
Next we are going to create a new C# script which will be placed within the Main Camera component. This new C# will house the ability to send Raycast from the main camera. For convenice sake, I decided to name this new script “Player”.
Next (if you haven’t done so already) we are going to make sure we have the New Input System packaged installed. We can check by going to our Package Manager and going through the list to see if you it installed. If it isn’t installed then you should download it from the Unity Registry, because getting use to the new Input System would be beneficial later on.
Once Unity restarts from using the New Input system, we are going to open up the new Player script and start working with Raycasting. Begin with accessing the Input System library so we can use the new features provided by the new Input System.
Then in the Update Method we are going to create an IF statement of checking whenever the left mouse button was clicked.
For the Raycast to actually work, we need to fulfill several conditions for the cast to be functional.
1. we need to have a staring origin from where the ray is initialized.
2. we then need a container to store the information of every object hit with the ray so we can then make use of the data. (aka. if the ray hits a cube, the player script needs to know that the ray hit the cube.)
3. then we can finally assign a function with the information gathered (aka, change the color of the cube that was just hit by the raycast)
For condition 1 we are going to use the mouse position from the main camera view. Basically whenever the mouse cursor is visiable within the main camera view, we will track its position.
To do that using the New Input System, we need to create a new local variable called “MousePos” (short for mouse position) and do a position readvalue. This will return a vector 2 value for the Mouse Position variable.
Then we can use the info as the ray cast origin (aka starting point). As you can see on the image below, we are using the Ray Origin from the Screen Point to Ray function on the Main Camera, and the value required is the Mouse position.
To solve for condition 2 we will need a container to store data which the cast will return with. This is the tasked of “RaycastHit”, this function which we will name “hitInfo” will be the variable to store all returned data from the raycast. With that finished, we can now start assigned a function with the RaycastHit information.
In this case we want to change the cube material color. With the hitInfo we can directly access the collider and get the component Mesh Renderer and we can alter the material color there.
To test this function out, we will now return back to Unity and play the scene to see if the raycast works. We need to make sure that each left mouse click doesn’t change the cube color unless the cursor is on top of the cube itself.
As you can see, this function works with multiple objects within the scene.
If we want to clean up the script a tad bit and make it more readable then we can create a new variable within the IF statement called “hit Object” which will store the hitInfo collider Mesh Renderer info.
Create a new IF statement and state whenever the hit Object is not null, we will then change the color. That way, we are certain that only when the raycast hits something that can return back information can we finally change the material color.