Shooting Using Raycasts
Objective: Set up Shooting
Follwing on from getting the Player to move and Camera to follow and look, we add some cubes with a green material. We will use these as targets while we set up shooting raycasts.
The camera look up and down was clamped(locked at certain levels) so the players view range up and down was limited.
To get a visual representation of target aiming, a UI Image was used and a crosshair source image used and locked to the center of the viewport(visable screen).
A new script called PlayerShoot was created and added to the Player. A conditional if statement to check for the the left mouse click was used to trigger a Shoot method. We use the Camera.main.ViewportPointToRay to cast to the center of the screen(center of the crosshair) using the values set in a Vector3 of 0.5f, 0.5f, 0. These values are associated with the viewport meaning for the X-axis, 0 would be the very left and 1 being the very right giving 0.5 as the center point. Therefore X of 0.5f and Y of 0.5f gives us the dead center. We then use a RaycastHit to store what we hit with the Ray from the camera origin. For now we just want to know we hit something so we log the name of the object with a collider that gets hit.