3D Survival FPS Game

Making the Center of the screen shoot RayCasts

Simon Truong
3 min readMar 28, 2022

--

Moving forward with working with the Reticule and Camera rotation clamp, it is time to actually make the center of the Reticule functional. We do this by casting rays from the center of the screen and information would be brought back whenever the ray hits something.

Scripting the RayCast:

To start we are first going to create a new C# script called “Shooting” then we are going to drag the script into the player game object.

Within the new script we are going to create a new method called “shoot” this method will control whenever the player left clicks the mouse. Using a IF statement we are going to get the Mouse Button 0 (0 is left, 1 is for right and 2 is for the middle scroll wheel). Next we are going to create a new Vector 3 called center, this will be the measurement of the center of the screen.

Next we are going to make another local variable called “Ray”, this will be the starting point of the Ray Cast, which will be at the center of the screen. It will also be where we link the ray cast to the camera movement.

Finally we are going to create the final local variable which is hit Info. Hit Info will be the data storage for what the ray cast hits and will be return back to the player.

(example: Shooting a ray out that hits a enemy, hit Info stores the enemy info, and the ray comes back to the player displaying the targeted object.)

In order to make all 3 of these local variables work, we are going to create a new IF statement which does exactly what the example above explains. Using the Debug.Log function we can also display what we have hit within the Unity console.

Now back in Unity, when you test out the game, you will noticed that whatever the ray cast targets, the console will return an value.

--

--

Simon Truong
Simon Truong

Written by Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

No responses yet