Zombie hordes, meet my little friend.
Serious Objective: Adding in the basics for being able to shoot at the zombies using ray tracing.
Sure it can be fun trying to make it through a level with no guns and just trying to sneak your way past everything but what about all the shooting? This is meant to be a 3rd person shooter!
Here we can use some ray casts to check if we hit the zombies. But first a little target practice? Add in some cubes into your scene and give them different names, this will be our targets for now, until the zombies come.
We can also add in a reticule, this is what we will be aiming through to know where we are shooting. First, add a UI image to your project.
Now we can find a reticule to have as our Image.
This will be the one I will be using.
After centering it on the canvas we should end up with something like this.
Next, we can make a new script to hold all the shooting.
After making the new script, we can attach it to the player and then open it up. In the update method, we can check if the player presses the left mouse button, if they do we can call a new method called shoot.
Now we can start on the ray casting. First, we want the center of the screen, we can do this by making a new vector 3 and assigning it a new vector 3 of 0.5,0.5,0 as the screen starts at 0 and goes to 1 so 0.5 is the middle.
Next, the ray. Here we can make a new ray and assign it to the camera.main.ViewportPointToRay(center of the viewport) and while we are at it, somewhere to store what we hit.
Lastly, we can cast the actual ray using Physics.Raycast(ray Origin, what we hit) then for now we will just write out a message saying what we hit.
now to test it out.
And there you have it, ray cast all set up and ready for some actual zombies to shoot!
This one was a starting point for getting the guns to be able to hit enemies, later we can add in gun models and animations of the gun firing and of course, actually being able to shoot the zombies! For now, that should do it though, See you in the next one!